Hi guys, I have come across this more than once (condition ? action_if_true: action_if_false I want to be able to understand it. Can someone give me a working example? Thanks, -Tony
normaly we can do this if (isset($_GET['parameter'])) { $param = $_GET['parameter']; } else { $param = ''; } Code (markup): but the same can be writen on one line as this $param = isset($_GET['parameter']) ? $_GET['parameter'] : ''; Code (markup): you can tell it like this $param IS is variable set $_GET['parameter'] YES WRITE $_GET['parameter'] NO WRITE ''
I have another question. Can this apply to doing something, instead of just setting a variable? I mean, could you shorten this? if($mine==false){echo 'User Page';}else{echo 'My User Page';} PHP: Thanks, -Tony
Okay... This? (assume foo and bar are functions, each with one parameter) if($mine==false) { foo('testvalue'); } else { bar(28); } PHP: Thanks, -Tony
this should be like this but when you want to return a variable to a string use the methods from above.
My opinion: I wouldn't use this method. Code will get more complicated. I like to keep my code as simple as possible and in a format that will work on every server etc.
Is there a way to spread this over multiple lines? (if I had long strings to echo) echo ($mine == false) ? "user page" : "my user page"; PHP: Just so I could see my code better, in a minimized window. Thanks, -Tony
I meant spreading the source code out. For example, could: $foo=true; echo ($foo==true) ? "this is such a crazily long string its so long its insane i want to spread it out in my code" : "shortstring"; PHP: be spread out to: $foo=true; echo ($foo==true) ? "this is such a crazily long string its so long its insane i want to spread it out in my code" : "shortstring"; PHP: without an error? Because $foo=true, I would want the above snippet to result in: this is such a crazily long string its so long its insane i want to spread it out in my code Would that work? Thanks, -Tony
So, like this? echo (($foo==true) ? "this is such a crazily long string its so long its insane i want to spread it out in my code" : "shortstring"); PHP: That would work fine? -Tony
If you need more than one line, I'd use HEREDOC to set a variable with that text, then echo the variable. It's neater. //from the manual [COLOR=#000000][COLOR=#0000BB]$str [/COLOR][COLOR=#007700]= <<<EOD [/COLOR][COLOR=#DD0000]Example of string spanning multiple lines using heredoc syntax. [/COLOR][COLOR=#007700]EOD; [/COLOR][/COLOR] [FONT=monospace][COLOR=#b1b100]echo[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#000088]$mine[/COLOR] [COLOR=#339933]==[/COLOR] [COLOR=#009900][B]false[/B][/COLOR][COLOR=#009900])[/COLOR] ? [COLOR=#0000ff]"user page"[/COLOR] [COLOR=#339933]:[/COLOR] $str[COLOR=#339933];[/COLOR] [/FONT] Code (markup):
Why HEREDOC? Why not just $str = 'Example of string spanning multiple lines using single quotes.'; PHP: -Tony