Hello,

$c = 'bool'; // boolean
settype($c, "integer"); // $c is now integer (1)
echo $c;

Why has $c become the int of 1 and not any other number ?
And why is the following set to an int of 0 and not any other number ?

$c = 'string'; // boolean
settype($c, "integer"); // $c is now integer (0)
echo $c;

I'm sorry, I'm not understanding your question. Whenever $c is a string, regardless of if the value of that string is bool or string, if you do settype($c, 'integer'); then the value of $c will be 0.

$c = 'blah';

settype($c, 'integer'); // Results in $c is now the integer 0
$c = intval($c); // Results in $c is now the integer 0
$c = (int)$c; // Results in $c is now the integer 0 (typecasting method)

@Dani

Thank you for the explanation.
Dow you prefer the 2nd or 3rd method from your 3 examples ?

Personally I use intval().

commented: Nowadays me too. Migrated from the 2nd example once I learnt about the intval. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.