Hello, I bump into this 2 codes and I didn't understand what they are doing and for what use can (or should I use) this type or php writing...? 1. $x = 100 == '100' ? 'yes' : 'no' ; echo $x; PHP: 2. $x = 100 === '100' ? 'yes' : 'no' ; echo $x; PHP: Please try to explain it to me in the abstractest way you can. Thank you in advance.
It's called Ternary Operation $x = 100 === '100' ? 'yes' : 'no'; Code (php): is they equivalent of if(100 === '100') { $x = 'yes'; } else { $x = 'no'; } Code (php): condition ? true : false; Because the identical === operator is used, data type will be taken into the comparison, resulting in $x having the string value of "no". Frustrating I can't post links properly yet. php .net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Thanks! I didn't got you on the '===' thing I understand that its check if its equal and have the same type. but who it check the type?
== is the equals operator, and === is the identical operator. e.g if('1' === 1) { echo '1'; } elseif('1' == 1) { echo '2'; } Code (php): That should output 2, '1' is a string and 1 is an integer, making them not identical, though they are equal.
OK, gotcha. Just 1 thing - "===" check equal and identical (according to: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary ) By the way - is there any different between "else" and "elseif" ?
Of course two identical values are also equal, it goes without saying. The difference between "else" and "elseif" is that elseif has an extra condition, else will execute if the above if/elseif evaluated false, elseif will execute if the above if/elseif is false, and the condition set is also true; else must also be last, you can't follow an elseif after an else.
Pretty much, $a=1; if($a==2) { echo "foo"; } else { echo "bar"; } //Echoes bar if($a==2) { echo "foo!"; } elseif($a==3){ echo "bar!"; } elseif($a==1){ echo "foobar!"; } //echoes foorbar! PHP: