php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #25243 casting to int behavior weird
Submitted: 2003-08-25 23:21 UTC Modified: 2003-08-27 17:18 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: elite_eagle67 at hotmail dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: Irrelevant OS: windoze
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: elite_eagle67 at hotmail dot com
New email:
PHP Version: OS:

 

 [2003-08-25 23:21 UTC] elite_eagle67 at hotmail dot com
Description:
------------
Do (int)$var where $var is an array that has some kind of value stored, and the return value will be 1, not 0.

Now this obviously doesn't make sense as you cast to (int) so you can see if a variable has a valid INTEGER variable, NOT if it is the integer equivilent of a * boolean * value.

Ofcourse, an empty array in this example will return 0, as would be expected behavior for a BOOLEAN cast, not integer howerever...

I'm unaware how (string) cast deals with this, possibly similiar but if so, then just apply this bug report to that ;)

Reproduce code:
---------------
$array = array( 'yay' => 'boo!' );

if((int)$array) {
   echo (int)$array;
} else {
   echo (int)$array;
}

Expected result:
----------------
0

Actual result:
--------------
1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-26 17:43 UTC] elite_eagle67 at hotmail dot com
Ok then I move this to a feature request. Im asking then that if the behavior of array to int, object to int, etc is predictable and in the manner I described

IE any non integer value will be converted to 0 when casted to int as you think it would be.

Thanks.
 [2003-08-27 05:41 UTC] derick@php.net
We will not break other people's scripts.
 [2003-08-27 17:18 UTC] elite_eagle67 at hotmail dot com
Oh.

Aren't you anyway though in PHP 5 with all the object stuff? The way you must first declare objects (I think) before you can use them. I think casting in this fashion is used even less then objects in those situations, or even any way that objects will brake old scripts.

Also it says right in the link posted that you should not rely on the casting to other types behavior and it can change without notice. So what's the point of that statement if in reality the PHP managment is doing everything they can to keep it the same?

Well anyway don't mean to seem rude or anything but I think it would be cool if casting worked as was most logical. I used to like to use it (and I think others might to) because it was a quick way to check variable validity for a certain purpose. is_numeric() is slower then casting. But I cant use it now cause it doesn't work.

Have a nice day ;)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 16:01:31 2024 UTC