$slcustom48 = (int)$slcustom48;
if ($slcustom48) { // will be better and don't allow $slcustom48 be == 0.
Your test will be passed by "123'; DROP TABLE myTable--" :)
$slcustom48 = (int)$slcustom48;
if ($slcustom48 > 0) { // even better
$slcustom48 = (int)$slcustom48;
if ($slcustom48) { // will be better and don't allow $slcustom48 be == 0.
Your test will be passed by "123'; DROP TABLE myTable--" :)
$slcustom48 = (int)$slcustom48;
if ($slcustom48 > 0) { // even better
>>> $foo = NULL
>>> $foo != 0
false
>>> $foo == 0
true
PHP treats variables with a NULL
value as the integer 0
with the ==
operator so the NULL
check is indeed redundant.
Your code doesn't even check if a value is greater than 0, which is your question, so no. My answer is no. It's not redundant. It's not correct.
$slcustom48=(bool)$slcustom48; if($slcustom48) { // do something } else { // do something else }
First of all, marcc is right: You're code doesn't check whether the variable holds a value greater than zero. It simply tests whether the variable is NOT zero (could be a negative number, but also a string or an object, etc.)
So expanding on that thought: you're not checking whether the variable holds an integer. What you DO check is whether the variable holds a value at all, with != NULL. But the order in which you have this in your if condition is the wrong way around. Here's why:
To prevent a notice in your error log (or appearing on screen on a test server) you need to check whether the variable holds anything at all BEFORE you check anything else. PHP will stop interpreting the conditional as soon as it returns false. This can be done also with PHP's isset() or indeed with != NULL.
Next, you need to make sure that the variable holds an integer (if that's important to you, which I think should be important to you IMHO).
Lastly, you want to check whether the value of the variable is larger than zero. So, to sum it up, this is what you need:
if( isset( $slcustom48 ) && is_int( $slcustom48 ) && $slcustom48 > 0 )
{
// the variable indeed holds an integer larger than zero
}