1--TEST-- 2String to number comparison 3--FILE-- 4<?php 5 6// This tests the examples from the RFC. 7 8function format($val) { 9 if (is_float($val)) { 10 if (is_nan($val)) return "NAN"; 11 if ($val == INF) return "INF"; 12 if ($val == -INF) return "-INF"; 13 } 14 return json_encode($val); 15} 16 17function compare_eq($val1, $val2) { 18 echo format($val1), " == ", format($val2), ": ", format($val1 == $val2), "\n"; 19} 20 21function compare_3way($val1, $val2) { 22 echo format($val1), " <=> ", format($val2), ": ", format($val1 <=> $val2), "\n"; 23} 24 25compare_eq(42, "000042"); 26compare_eq(42, "42.0"); 27compare_eq(42.0, "+42.0E0"); 28compare_eq(0, "0e214987142012"); 29echo "\n"; 30 31compare_eq("42", "000042"); 32compare_eq("42", "42.0"); 33compare_eq("42.0", "+42.0E0"); 34compare_eq("0", "0e214987142012"); 35echo "\n"; 36 37compare_eq(42, " 42"); 38compare_eq(42, "42 "); 39compare_eq(42, "42abc"); 40compare_eq(42, "abc42"); 41compare_eq( 0, "abc42"); 42echo "\n"; 43 44compare_eq(INF, "INF"); 45compare_eq(-INF, "-INF"); 46compare_eq(NAN, "NAN"); 47compare_eq(INF, "1e1000"); 48compare_eq(-INF, "-1e1000"); 49echo "\n"; 50 51$float = 1.75; 52 53echo "precision=14:\n"; 54ini_set('precision', 14); 55compare_3way($float, "1.75abc"); 56compare_3way((string) $float, "1.75abc"); 57 58echo "precision=0:\n"; 59ini_set('precision', 0); 60compare_3way($float, "1.75abc"); 61compare_3way((string) $float, "1.75abc"); 62 63?> 64--EXPECT-- 6542 == "000042": true 6642 == "42.0": true 6742 == "+42.0E0": true 680 == "0e214987142012": true 69 70"42" == "000042": true 71"42" == "42.0": true 72"42.0" == "+42.0E0": true 73"0" == "0e214987142012": true 74 7542 == " 42": true 7642 == "42 ": true 7742 == "42abc": false 7842 == "abc42": false 790 == "abc42": false 80 81INF == "INF": true 82-INF == "-INF": true 83NAN == "NAN": false 84INF == "1e1000": true 85-INF == "-1e1000": true 86 87precision=14: 881.75 <=> "1.75abc": -1 89"1.75" <=> "1.75abc": -1 90precision=0: 911.75 <=> "1.75abc": 1 92"2" <=> "1.75abc": 1 93