Jump to content

? and : in PHP


Naez

Recommended Posts

Well this is called ternary opreator works like If then else

 

consider this

 

<?php

 

( $a>=$b ) ? echo"$a is greater then $b " : echo "$a is less then b";

?>

 

well before "?" anycondition is placed which could be either true or false if the condition is true then it will come to the immediate next part mean after "?" and if false then it will go to ":" part hope this makes clear

Cheers

Link to comment
https://forums.phpfreaks.com/topic/94136-and-in-php/#findComment-482225
Share on other sites

It's very useful for printing stuff. Baabu's code can be rewritten as:

echo ( $a >= $b ) ? "$a is greater then $b " : "$a is less then b";

 

Tip: don't ever nest ternary operators, it gets way too confusing to read. And try to keep everything simple - if you're outputting long strings use if/else instead.

Link to comment
https://forums.phpfreaks.com/topic/94136-and-in-php/#findComment-482337
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.