note 79146 added to language.types.string
| From: | qriz at example dot com | Date: | Tue, 13 Nov 2007 17:54:50 +0000 |
| Subject: | note 79146 added to language.types.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132230@lists.php.net to get a copy of this message | ||
<?php
$a = '2007-11-05 15:17:49';
$b = '2007-11-05 15:17:48';
$bool = $a > $b;
var_dump($bool); //bool(true)
?>
works correctly since there is a comparing between strings. The comparisson is done on the last
number/letter (since thats the only thing that is difference in the string) and that is in this
case: the 9 and 8.
8 > 9 = true
if you want to compare the string as pure numbers then you must type cast it to numbers or type
juggle it:
<?php
$a = '2007-11-05 15:17:49';
$b = '2007-11-05 15:17:48';
$bool1 = ($a + 0) > ($b + 0); // 2007 > 2007
$bool2 = (int) $a > (int) $b; // 2007 > 2007
$bool3 = intval($a) > intval($b); // 2007 > 2007
var_dump($bool1,$bool2,$bool3); //bool(false)
?>
----
Server IP: 85.12.26.134
Probable Submitter: 82.171.170.147
----
Manual Page -- http://www.php.net/manual/en/language.types.string.php
Edit -- https://master.php.net/note/edit/79146
Del: integrated -- https://master.php.net/note/delete/79146/integrated
Del: useless -- https://master.php.net/note/delete/79146/useless
Del: bad code -- https://master.php.net/note/delete/79146/bad+code
Del: spam -- https://master.php.net/note/delete/79146/spam
Del: non-english -- https://master.php.net/note/delete/79146/non-english
Del: in docs -- https://master.php.net/note/delete/79146/in+docs
Del: other reasons-- https://master.php.net/note/delete/79146
Reject -- https://master.php.net/note/reject/79146
Search -- https://master.php.net/manage/user-notes.php