1--TEST-- 2Test <=> operator : different types 3--FILE-- 4<?php 5$valid_true = array(1, "1", "true", 1.0, array(1)); 6$valid_false = array(0, "", 0.0, array(), NULL); 7 8$int1 = 679; 9$int2 = -67835; 10$valid_int1 = array("678", "678abc", " 678", "678 ", 678.0, 6.789E2, "+678", +678); 11$valid_int2 = array("-67836", "-67836abc", " -67836", "-67836 ", -67835.0001, -6.78351E4); 12$invalid_int1 = array(679, "679"); 13$invalid_int2 = array(-67835, "-67835"); 14 15$float1 = 57385.45835; 16$float2 = -67345.76567; 17$valid_float1 = array("57385.45834", "57385.45834aaa", " 57385.45834", 5.738545834e4); 18$valid_float2 = array("-67345.76568", "-67345.76568aaa", " -67345.76568", -6.734576568E4); 19$invalid_float1 = array(57385.45835, 5.738545835e4); 20$invalid_float2 = array(-67345.76567, -6.734576567E4); 21 22 23$toCompare = array( 24// boolean test will result in both sides being converted to boolean so !0 = true and true is not > true for example 25// also note that a string of "0" is converted to false but a string of "0.0" is converted to true 26// false cannot be tested as 0 can never be > 0 or 1 27 true, $valid_false, $valid_true, 28 $int1, $valid_int1, $invalid_int1, 29 $int2, $valid_int2, $invalid_int2, 30 $float1, $valid_float1, $invalid_float1, 31 $float2, $valid_float2, $invalid_float2 32); 33 34$failed = false; 35for ($i = 0; $i < count($toCompare); $i +=3) { 36 $typeToTest = $toCompare[$i]; 37 $valid_compares = $toCompare[$i + 1]; 38 $invalid_compares = $toCompare[$i + 2]; 39 40 foreach($valid_compares as $compareVal) { 41 if (($typeToTest <=> $compareVal) === 1) { 42 // do nothing 43 } 44 else { 45 echo "FAILED: ('$typeToTest' <=> '$compareVal') !== 1\n"; 46 $failed = true; 47 } 48 if (($compareVal <=> $typeToTest) === -1) { 49 // do nothing 50 } 51 else { 52 echo "FAILED: ('$compareVal' <=> '$typeToTest') !== -1\n"; 53 $failed = true; 54 } 55 if (($compareVal <=> $compareVal) === 0) { 56 // do nothing 57 } 58 else { 59 echo "FAILED: ('$compareVal' <=> '$compareVal') !== 0\n"; 60 $failed = true; 61 } 62 } 63 64 foreach($invalid_compares as $compareVal) { 65 if (($typeToTest <=> $compareVal) === 1) { 66 echo "FAILED: ('$typeToTest' <=> '$compareVal') === 1\n"; 67 $failed = true; 68 } 69 if (($compareVal <=> $typeToTest) === -1) { 70 echo "FAILED: ('$compareVal' <=> '$typeToTest') === -1\n"; 71 $failed = true; 72 } 73 if (($compareVal <=> $compareVal) !== 0) { 74 echo "FAILED: ('$compareVal' <=> '$compareVal') !== 0\n"; 75 $failed = true; 76 } 77 } 78 79 if (($typeToTest <=> $typeToTest) === 0) { 80 // do nothing 81 } 82 else { 83 echo "FAILED: ('$typeToTest' <=> '$typeToTest') !== 0\n"; 84 $failed = true; 85 } 86} 87if ($failed == false) { 88 echo "Test Passed\n"; 89} 90?> 91===DONE=== 92--EXPECT-- 93Test Passed 94===DONE=== 95