Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.