note 64013 added to function.strcmp
| From: | robertb at littleNOSPAMcutie dot net | Date: | Wed, 05 Apr 2006 14:19:20 +0000 |
| Subject: | note 64013 added to function.strcmp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107386@lists.php.net to get a copy of this message | ||
S dot Radovanovic suggested:
"So my conclusion is that when comparing string, you'd better not make use of == (use
strmp or === instead). For integer comparisons the == equation can be usefull, since our values will
always be casted to an integer (1 == "1" returns true)."
When I tried this, I ran into a problem with parameters passed in the URL. I forgot that a
parameter that is not supplied wouldn't be a string, so this code -- which I expected to
default to the current year -- didn't work:
<?php
// Did we get a parameter?
if ($year === '') {
// Default to current year
$temp = getdate();
$year = $temp['year'];
}
$fname = "magic{$year}.txt";
if (file_exists($fname) == FALSE) {
echo "<h2>Data file doesn't exist</h2>";
exit;
}
?>
Since $year was uninitialized, it didn't exactly equal '', and the file wasn't
found. I went back to == in this case.
However, I did heed your advice when looking for a particular value ($debug === 'Y'),
instead of looking for the lack of a value. Thanks!
----
Server IP: 64.71.164.2
Probable Submitter: 164.57.122.126
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.strcmp.php
Edit -- http://master.php.net/note/edit/64013
Del: integrated -- http://master.php.net/note/delete/64013/integrated
Del: useless -- http://master.php.net/note/delete/64013/useless
Del: bad code -- http://master.php.net/note/delete/64013/bad+code
Del: spam -- http://master.php.net/note/delete/64013/spam
Del: non-english -- http://master.php.net/note/delete/64013/non-english
Del: in docs -- http://master.php.net/note/delete/64013/in+docs
Del: other reasons-- http://master.php.net/note/delete/64013
Reject -- http://master.php.net/note/reject/64013
Search -- http://master.php.net/manage/user-notes.php