Examples:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php var_dump(0 == "a" ); // 0 == 0 -> true var_dump( "1" == "01" ); // 1 == 1 -> true var_dump( "10" == "1e1" ); // 10 == 10 -> true var_dump(100 == "1e2" ); // 100 == 100 -> true switch ( "a" ) { case 0: echo "0" ; break ; case "a" : // never reached because "a" is already matched with 0 echo "a" ; break ; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php // Integers echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floats echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings echo "a" <=> "a" ; // 0 echo "a" <=> "b" ; // -1 echo "b" <=> "a" ; // 1 echo "a" <=> "aa" ; // -1 echo "zz" <=> "aa" ; // 1 // Arrays echo [] <=> []; // 0 echo [1, 2, 3] <=> [1, 2, 3]; // 0 echo [1, 2, 3] <=> []; // 1 echo [1, 2, 3] <=> [1, 2, 1]; // 1 echo [1, 2, 3] <=> [1, 2, 4]; // -1 // Objects $a = (object) [ "a" => "b" ]; $b = (object) [ "a" => "b" ]; echo $a <=> $b ; // 0 $a = (object) [ "a" => "b" ]; $b = (object) [ "a" => "c" ]; echo $a <=> $b ; // -1 $a = (object) [ "a" => "c" ]; $b = (object) [ "a" => "b" ]; echo $a <=> $b ; // 1 // only values are compared $a = (object) [ "a" => "b" ]; $b = (object) [ "b" => "b" ]; echo $a <=> $b ; // 1 ?> |
Boolean/null comparison
1 2 3 4 5 6 7 8 | <?php // Bool and null are compared as bool always var_dump(1 == TRUE); // TRUE - same as (bool)1 == TRUE var_dump(0 == FALSE); // TRUE - same as (bool)0 == FALSE var_dump(100 < TRUE); // FALSE - same as (bool)100 < TRUE var_dump(-10 < FALSE); // FALSE - same as (bool)-10 < FALSE var_dump(min(-100, -10, NULL, 10, 100)); // NULL - (bool)NULL < (bool)-100 is FALSE < TRUE ?> |
Transcription of standard array comparison
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php // Arrays are compared like this with standard comparison operators function standard_array_compare( $op1 , $op2 ) { if ( count ( $op1 ) < count ( $op2 )) { return -1; // $op1 < $op2 } elseif ( count ( $op1 ) > count ( $op2 )) { return 1; // $op1 > $op2 } foreach ( $op1 as $key => $val ) { if (! array_key_exists ( $key , $op2 )) { return null; // uncomparable } elseif ( $val < $op2 [ $key ]) { return -1; } elseif ( $val > $op2 [ $key ]) { return 1; } } return 0; // $op1 == $op2 } ?> |
Assigning a default value
Another conditional operator is the "?:" (or ternary) operator.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php // Example usage for: Ternary Operator $action = ( empty ( $_POST [ 'action' ])) ? 'default' : $_POST [ 'action' ]; // The above is identical to this if/else statement if ( empty ( $_POST [ 'action' ])) { $action = 'default' ; } else { $action = $_POST [ 'action' ]; } ?> |
Non-obvious Ternary Behaviour
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php // on first glance, the following appears to output 'true' echo (true? 'true' :false? 't' : 'f' ); // however, the actual output of the above is 't' // this is because ternary expressions are evaluated from left to right // the following is a more obvious version of the same code as above echo ((true ? 'true' : false) ? 't' : 'f' ); // here, you can see that the first expression is evaluated to 'true', which // in turn evaluates to (bool)true, thus returning the true branch of the // second ternary expression. ?> |
Please login to continue.