At the moment, I'm the only one posting in this forum anyways so I'm not trying to bump this thread...but I discovered something about my question above that might be of interest in helping to answer it...
The above code displays "zero". Alright '0' does not equal 'x'.
But why does (int)0 equal 'x' in the first script? I didn't think I'd have to type cast an integer when not using quotes and explicitly stating the value. So I'm confused as to the behavior of the first 4 code blocks I've posted here.
Any ideas would be helpful because it's holding back some validation I'm trying to run in my scripts.
Because of PHP's loose typing, where it's casting 'x' to integer so that it can compare both expressions as the same type, and (int) 'x' will evaluate to 0. If you don't want that loose typing, then use the "===" (is identical to) operator, which checks for both same type and value, instead of the "==" operator.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Rhetorical questions:
Why does it cast 'x' to an integer (resulting in true) instead of casting 0 to a string (resulting in false)? For example, ('x'=='y') evaluates to false. So it's not casting each of those to int.
Actual question:
If I have two numbers I am comparing, for example, and I don't know if they are floats, ints, etc., is it common practice to type cast them in the expression as in below?
PHP Code:
if((float)$a==(float)$b){ //... }
The above code may not evaluate the same as
PHP Code:
if($a===$b){ //... }
because who knows what $a or $b will be cast to?
Last edited by speghettiCode; 01-12-2013 at 06:16 PM.
Reason: Qualify the question
In your first example, anything that can be considered a zero (0, 0.0, "0", NULL, FALSE, "") will be cast to float as 0.0, so if $a is FALSE and $b is "", then they would both be cast to 0.0 and the comparison would be true -- which may or may not be what you want to happen. If you want to make sure you are comparing numbers, you might want to test each value with is_numeric() first, and if either test fails throw an exception or whatever other error-handling you want to do.
Or you could insist that those variables be floats (or whatever), and test them accordingly with is_float(), etc; But I don't think there's a one size fits all solution, since many times loose typing and type-juggling works fine.
If it becomes really critical, you could even create classes for specific types, which you could then pass into function parameters with type-hinting.
PHP Code:
<?php
abstract class Type
{
private $value;
public function __construct($value=null)
{
if($value !== null) {
$this->set($value);
}
}
public abstract function set($value);
public function get()
{
return $this->value;
}
}
class FloatType extends Type
{
public function set($value)
{
if( ! is_numeric($value)) {
throw new Exception ("'$value' is not numeric");
}
$this->value = (float)$value;
}
}
function foo(FloatType $bar)
{
$result = $bar->value . " is " . (is_float($bar->value) ? '' : 'not ') . "a float";
return $result;
}
That's likely overkill for most applications, but something along that lines might be quite useful when you really need to keep your variable types in order.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks