WTFZane WTFZane - 6 months ago 29
PHP Question

php - String to int not working properly and just produce zero

I've looked everywhere in the web and do some testing and I can't still understand my code.

$hcptype_id = $_GET["asdfghjkl"];
$a = 0 + $hcptype_id;
echo $a;


The
$a
produces 0 although the
$hcptype_id
has a value of 4. I've also tried the
(int)$hcptype_id
but still produces 0.

EDITED

echo $hcptype_id;
echo $_GET["asdfghjkl"];
var_export($_GET["asdfghjkl"]);


Produces:

"4"
"4"
'"4"'


EDITED AGAIN

var_dump($_GET);
var_dump($hcptype_id);
var_dump($a);


Produces:

array(1) { ["asdfghjkl"]=> string(3) ""4"" } string(3) ""4"" int(0)


EDITED MUCH AGAIN

The variable
asdfghjkl
comes from another page and it goes this
<a href=sc_hcp_type_edit.php?asdfghjkl="'.$row['hcptype_id'].'">

Answer

As you can see in your different traces, there are double-quotes (") that prevent it from being parsed as an integer.

One solution is to use trim() to remove the offending character: $hcptype_id = trim($_GET["asdfghjkl"], '"');.