php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17698 explicit casting of string not functioning properly
Submitted: 2002-06-11 09:41 UTC Modified: 2002-06-11 10:00 UTC
From: manderson at dsrglobal dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.1.2 OS:
Private report: No CVE-ID: None
 [2002-06-11 09:41 UTC] manderson at dsrglobal dot com
When comparing two strings for equality they appear to be, if possible, being converted to integers prior to the comparison even when explicitly cast.

The following script should demonstrate the issue:

<?php

if("0123" == "123") {
    echo "\"0123\" == \"123\"<br />\n";
}

if("0123" != "123") {
    echo "\"0123\" != \"123\"<br />\n";
}

if((string) "0123" == (string) "123") {
    echo "(string) \"0123\" == (string) \"123\"<br />\n";
}

if((string) "0123" != (string) "123") {
    echo "(string) \"0123\" != (string) \"123\"<br />\n";
}

if(!strcasecmp("0123", "123")) {
    echo "strcasecmp(\"0123\", \"123\") returns equal<br />\n";
} else {
    echo "strcasecmp(\"0123\", \"123\") returns not equal<br />\n";
}

?>

This script will echo :
<output>
"0123" == "123"
(string) "0123" == (string) "123"
strcasecmp("0123", "123") returns not equal
</ouput>

Obviously "0123" is not the same as "123".

<configuration>
'./configure' '--with-mysql' '--with-xml' '--with-apxs' '--with-config-file-path=/etc/httpd'
</configuration>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-11 09:46 UTC] sander@php.net
This is not a bug. PHP has no real types and tries to decide which type it is. Use === or !== to check for type too.
 [2002-06-11 09:57 UTC] manderson at dsrglobal dot com
What is the purpose of "(string)"?
 [2002-06-11 10:00 UTC] manderson at dsrglobal dot com
corrected "From" email address
 [2003-06-04 02:04 UTC] simon at esoft dot dk
I do believe this is a bug nevertheless! When you specificly typecast a 
value to a certain type you expect it to be compared without being cast 
back to Integer afterwards. 
 
Quote from the PHP manual about "Strings" 
"You can convert a value to a string using the (string) cast, or the 
strval() function. String conversion is automatically done in the scope 
of an expression for you where a string is needed. This happens when 
you use the echo() or print() functions, or when you compare a variable 
value to a string." 
 
I like to point out the last line, "when you compare a variable to a 
string", this is exactly what you do, so the fact is that comparing to a 
constant string would work but not a variable? This makes no sense.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 18:01:24 2019 UTC