php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52919 if does not work properly of strlen even if typecasted
Submitted: 2010-09-23 22:46 UTC Modified: 2010-09-24 05:35 UTC
From: ghazanfar dot ahmed at magsnet dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: Centos 5.5
Private report: No CVE-ID: None
 [2010-09-23 22:46 UTC] ghazanfar dot ahmed at magsnet dot net
Description:
------------
IF does not produce correct result when strlen is used to calculate length of a 
string which is an integer

Test script:
---------------
//IF does not seem to evaluate properly

$row['number'] = 700; //in my case if was value retrieved from mysql

// force typecasting
$number = (string)$row['number'];

if(strlen(trim($number)) > 7) {
  print "CHECK IF"; }
else {
  print "CHECK ELSE";
}

//It will always print "CHECK ELSE" where strlen($number)=3 (computes correctly)
// where 3 > 7 is false






Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-23 22:50 UTC] ghazanfar dot ahmed at magsnet dot net
Sorry, it prints "CHECK IF" where it should print "CHECK ELSE"
 [2010-09-23 23:54 UTC] felipe@php.net
This makes no sense... No way to reproduce such thing.
 [2010-09-24 03:36 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2010-09-24 03:36 UTC] cataphract@php.net
Expected behavior. 3 > 7 is false, hence the else block will be executed...
 [2010-09-24 04:01 UTC] cataphract@php.net
-Status: Bogus +Status: Open
 [2010-09-24 04:01 UTC] cataphract@php.net
Sorry, I didn't read the comment. Re-opened.
 [2010-09-24 05:35 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-09-24 05:35 UTC] aharvey@php.net
Works for me; as Felipe says, there doesn't seem to be a way to reproduce this. The sample code prints "CHECK ELSE" on every install I have to hand, which is exactly what I'd expect, given strlen("700") is fairly obviously 3.

I'd suggest double checking that all your values in your actual code are actually what you think they are and following up with a user support channel such as an appropriate mailing list, IRC, or Stack Overflow.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 16:02:26 2025 UTC