Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 vr4lyfe

vr4lyfe
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 July 2006 - 05:55 AM

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?

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 07 July 2006 - 06:14 AM

If I'm not wrong, you are using Concise Conditional.
There's an example over http://www.cwhnetwor...?showtopic=3243 if you are interested.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 vr4lyfe

vr4lyfe
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 July 2006 - 06:51 AM

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'; "

#4 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 07 July 2006 - 08:11 AM

No, for that you would have to use an if-else construct.
Cook

#5 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 07 July 2006 - 08:36 AM

I would also suggest you to use if statement, it's much more easier to use.

// Offtopic
Hey Cook, are your Surname Guo ?
Mine also :P

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 July 2006 - 01:47 PM

This is the Ternary Operator and is described in the PHP Manual at http://us3.php.net/m...parison.ternary

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

Ken

#7 vr4lyfe

vr4lyfe
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 July 2006 - 06:41 PM

Thanks guys.  Consider me informed on ternary operators / concise conditionals now.

;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users