note 79504 modified in function.is-int by bjori

From: Date: Wed, 05 Dec 2007 05:23:20 +0000
Subject: note 79504 modified in function.is-int by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-132833@lists.php.net to get a copy of this message
a small modification to the isInt checks provided by lclkk at urbanvagabond dot net to make them work for scientific notation integers that are provided in string form: function myIsInt ($x) { return (is_numeric($x) ? intval($x+0) == $x : false); } function Test($x) { echo "$x is " . ( myIsInt($x) ? ('an integer. The integer value is ' . intval($x+0)) : 'not an integer.'); echo "\n"; } Note the additional adding of 0 to $x in both functions. This would allow the following tests to return true: Test('53.45e2'); // 5345 Test('53.45e3'); // 53450 and this one to return false: Test('53.45e1'); // 534.5 --was-- a small modification to the isInt checks provided by lclkk at urbanvagabond dot net to make them work for scientific notation integers that are provided in string form: function myIsInt ($x) { return (is_numeric($x) ? intval($x+0) == $x : false); } function Test($x) { echo "$x is " . ( myIsInt($x) ? ('an integer. The integer value is ' . intval($x+0)) : 'not an integer.'); echo "\n"; } Not the additional adding of 0 to $x in both functions. This would allow the following tests to return true: Test('53.45e2'); // 5345 Test('53.45e3'); // 53450 and this one to return false: Test('53.45e1'); // 534.5 http://php.net/manual/en/function.is-int.php

« previous php.notes (#132833) next »