Click to See Complete Forum and Search --> : What is this shot hand for an If statement


solidariti
09-21-2005, 06:28 AM
? 'succeeded' : 'failed';

My thinking is if succeeded : or failed am I right????

I searched on google, and asked a few administrators but they havent seen it before.

Please help.

bathurst_guy
09-21-2005, 07:42 AM
1 == 2 ? echo "I cant count" : echo "I can count";

solidariti
09-21-2005, 08:26 AM
This doesnt seem to work, it says that there is an unexpected T_ECHO I've tried putting a semi colon between the two echo statements and : but that doesnt seem to work.

So its like a shortened if statement then?

NogDog
09-21-2005, 03:00 PM
echo (1 == 2) ? "I can't count" : "I can count";

NogDog
09-21-2005, 03:03 PM
PS: It is called the "ternary operator". More info on this page: http://www.php.net/manual/en/language.operators.comparison.php
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Stephen Philbin
09-21-2005, 03:19 PM
Aye, weirdly, using the sort-hand if structure seems to return the selected result rather than execute it as a standard if structure would do.

felgall
09-21-2005, 03:34 PM
A = B ? C : D;

is equivalent to

if (B) {A = C;} else {A = D;}

pyro
09-21-2005, 03:39 PM
Aye, weirdly, using the sort-hand if structure seems to return the selected result rather than execute it as a standard if structure would do.You are only supposed to use the ternary opperator when you want to return a value. ;)

SpectreReturns
09-22-2005, 12:02 AM
And by return he doesn't mean using 'return'. He means returning a value to use later on in the script (but can be used for 'return' if you want).


$test = ((true) ? "true" : "false"); // good use
((true) ? echo("hello") : print("sup")); // Bad use

kazotech
09-22-2005, 12:17 AM
normal if statement
if ($a == $b)
{
echo "THIS IS VALID";
}
else
{
echo "THIS IS NOT VALID":
}

short hand for writing if statement
echo ($a==$b) ? "THIS IS VALID" : "THIS IS NOT VALID" ;