Jump to content

$ x ? 0 : $ x ... syntax help


vr4lyfe

Recommended Posts

Hi guys.  I am readying some PHP code and it is using this syntax.

$x->var = $ x ? 0 : $ x;

I know what most its supposed to do (kinda), but just confused about exact syntax of the "?" and ":" notations.

Tried searching PHP.net but couldnt find what I was looking for.  Any expect care to point me to a page I can read or explain this to me?
Link to comment
https://forums.phpfreaks.com/topic/13926-x-0-x-syntax-help/
Share on other sites

Thanks for the quick reply.  I googled it .. and it seems a more common name would be the ternary operator.

Very good video on ternary operators @ vtc.com .. its one of their free php tutorial vids. 

Its seem though that these things are good only for echo .. lol .. perhaps its just happens to seem that way because all the example is jsut echo example.

Can I do something like

"(conditional statement) ? function_call(); some_other_functions(); another_function(); : print_error() ;"

instead of just

"(conditional statement) ? 'some text' : 'some other text'; "
Link to comment
https://forums.phpfreaks.com/topic/13926-x-0-x-syntax-help/#findComment-54253
Share on other sites

This is the Ternary Operator and is described in the PHP Manual at http://us3.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

Sometimes it's easier to use an if - then - else, sometimes the ternary operator, and sometimes a switch statement.

Ken
Link to comment
https://forums.phpfreaks.com/topic/13926-x-0-x-syntax-help/#findComment-54380
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.