I have this object
$data = simplexml_load_string('<xml><admin>0</admin></xml>');
where $data->admin = "0" (string)
. So with a string 0, the following would always return true
if($data->admin) {
echo 'is admin';
}
By casting the variable, this would work correctly
if((int)$data->admin) {
echo 'is admin';
}
But I'm still wondering if it's possible to change the data type inside the object (like doing so inside an array) so I don't have to do type casting.
Not with SimpleXML. What you can do is use DOMDocument instead. Using SimpleXML isn't recommended.
If string is '0' it will return false. If you cast it to int it will still return false. ) And at the end logical expressions are cast to boolean. So what you trying to do is useless)
there is no way to do so in PHP, instead of typecast it you could just try this solution
if ($data->admin != 0){
//is admin
}
You can also try this) Though I don't think its good way) The best here is compare again zero and not to make typecasting. If you want typecasting you can do following. But its not good practice to make 3 operations instead of 1 )
$data = simplexml_load_string('<xml><admin>0</admin></xml>');
if(strip_tags($data->admin->asXML()))
{
//is admin
}