Here is the simple code:
$result = 0;
$result = $obj->delete($this_id);
echo "Result:" . $result;
var_dump($result);
if ( (int) $result < 0 || $result == null ) {
echo "Here" . $result;
var_dump($result);exit;
}
Here is the result:
Result:0int(0)
Here0int(0)
Its not supposed to enter into if block. Because $result is = 0. Not < 0.
Am I missing something or PHP handles this differently?
The comparison to null should be === instead of ==. Since null can evalulate to 0, the comparision evaluates (0 == null) = true
if ( (int) $result < 0 || $result === null ) {
See http://php.net/manual/en/language.operators.comparison.php for more information
It looks like the cast to an int type is making your variable test incorrectly against 0.
Replace if( (int) $result ... with if( $result ...
Its not the first validation which matches:
var_dump(0 == null); //true
var_dump(false == null); //true
Related
So I just did some random test and understand the fundamentals of Precedence and the || and or operators but I'm having trouble understanding why $f changes:
$f = 0 || 1;
if ($f === 1){
echo "TRUE - $f";
}else{
echo "FALSE - $f";
}
$f = 0 or 1;
if ($f === 0){
echo "TRUE - $f";
}else{
echo "FALSE - $f";
}
Thanks for some insight.
What you are doing is the same as :
if (($f = 0) or 1){
// $f is being assigned the value 0, and the condition evaluates 0 or 1,
// 1 being equivalent to true, the condition is always true.
echo "TRUE - $f";
}else{
echo "FALSE - $f";
}
and
if ($f = (0 || 1)){ // which gives $f = true which returns true
echo "TRUE - $f";
}else{
echo "FALSE - $f";
}
if you want to check if $f is equal to a value or another you would do
if ($f === 0 or $f === 1)
Be aware that in php, by default an int 1 will be evaluated to bool true unless you do a strict comparison === or !==
It's normal to evaluate always to True. The reason is that OR means if that one of the values is True it will take this one.
Update to your new question:
The answer is that "||" has a greater precedence than "or"
// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;
// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;
You can learn more at the PHP manual website here which I found this example
the problem is here: "if ($f = 0 or 1){" :
"=" means you give the variable $f the value 0
You should use :
"==" : checks if the values are equal
"===" : checks if the left variable is identical to the right variable (value, type, etc.)
I have a variable $allrule = '(1 == 2) && (2 == 2)';
when i check if($allrule), it returns true because $allrule is treated as string. So i want to convert $allrule as condition of if statement. how can we do it.
This solution uses eval() which is pure evil, but since it wasn't stipulated not to do so...
$allrule = '(1 == 2) && (2 == 2)';
$result = eval("return (".$allrule.");"); // $result will be false
Expanded Example*:
$allrule = "(1433861812 > 1433694000) && (1433861812 > 1433771400) && (1433861812 > 1433944200)";
$result = eval("return (".$allrule.");");
if($result) {
echo "true";
} else {
echo "false"; // will echo "false"
}
*from comments
I'd like to know what is the best way to check whether a numeric string is positive or negative. I am using this answer on S.O but I am not able to determine whether a number with decimals is + or -
For example :
function check_numb($Degree){
if ( (int)$Degree == $Degree && (int)$Degree > 0 ) {
return 'Positive';
} else {
return 'Negative';
}
}
print check_numb("+2"); // returns Positive
print check_numb("+2.0"); // returns Positive
print check_numb("+2.1"); // returns Negative ??
print check_numb("+0.1"); // returns Negative ??
print check_numb("-0.1"); // returns Negative
It seems when a decimal is added it returns false. How do you properly check for positive strings > +0.XX and negative < -0.XX which 2 decimals..
Thanks!
Considering the given input, why not :
$number = '+2,33122';
$isPositive = $number[0] === '+' ? 'Positive' : 'Negative';
// Positive
$number = '-0,2';
$isPositive = $number[0] === '+' ? 'Positive' : 'Negative';
// Negative
Here is a more generic code, working even if the sign is removed from your input :
function checkPositive ($number) {
if (!is_numeric(substr($number, 0, 1))) {
$sign = substr($number, 0, 1);
return $sign == '+';
}
return $number > 0;
}
$number = '+2,33122';
var_dump(checkPositive($number));
// true
$number = '-2,33122';
var_dump(checkPositive($number));
// false
$number = '2,22';
var_dump(checkPositive($number));
// true
Your problem is because: (int)"+2.1" == 2 and 2 != "+2.1". For ALL numbers, if it is < 0 it is negative and if it is > 0 it is positive. If it is == 0, then it is obviously 0 which is unsigned.
function check_numb($Degree){
if ( $Degree > 0 ) {
return 'Positive';
} elseif( $Degree < 0 ) {
return 'Negative';
}
}
I want to do something like this:
if( $a = 'something' && $b = substr( $a, 2 ) )
{
//do something
}
I mean, on an if condition, evaluate two conditions, and the second one passing the first assigned $a as a parameter to the second condition function substr().
It is just an example, so I don't want answers to this functionality, just generic answers.
The above code throws 'Undefined' $a, since $a is not still assigned.
I could do the next:
if( $a = 'something')
{
if( $b = substr( $a, 2 ) )
//do something
}
}
but this will make my code bigger.
Is there any way to achieve something like the first example?
Edit:
I don't want to compare. Just assign and ensure that $a and $b are not null, false, ...
Your only problem is the wrong precedence of the && and = operators. This works just fine:
if (($a = 'something') && $b = substr($a, 2))
This way, $a is undefined:
if ($a = 'something' && $b = substr($a, 2))
But if you give the = operator priority:
if (($a = 'something') && $b = substr($a, 2))
It will be set.
Moreover, you can simply write:
if( $b = substr( $a = 'something', 2 ) )
This question intrigued me along with #moonwave99 answer, so I did some testing with his last answer.
if( $b = substr( $a = NULL, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = FALSE, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = 0, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = TRUE, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = 233, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
PASS
if( $b = substr( $a = "SOMETHING", 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
PASS
The only way to get it to fail was to pass the Boolean TRUE. But if you are expecting string values, it should fail all Boolean values, zero and NULL and evaluate to true on ints, floats, and string values. (Haven't tested with array, but I suspect it would fail for any non-primitive types). Interesting question.
Use isset() for that.Also keep in mind use == or === for comparison operations since = is assignment operator
if( (isset($a) && $a == 'something') && (isset($b) && $b == substr( $a, 2 )) )
{
//do something
}
If I want to check the variable I need to do this:
if ( $i != '' || $i != 0 || $i != NULL ) {
// ...do some code
}
Could these 3 checks be somehow merged into 1 via some php function or a trick?
if (!empty($i)) {
// ... do some code
}
Please see http://php.net/manual/function.empty.php
All of them are actualy falsey. So you could do
if(!$i) {
}
http://php.net/manual/en/language.types.boolean.php
I should probably elaborate on why the OP has some bad assumptions. The list of things PHP will evaluate to false is long
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
This is where equivalency comes into play. It's a comparison operator. The OP has this
if ( $i != '' || $i != 0 || $i != NULL )
All three are actually the same test. Take this code
$i = 0;
if($i == '') {
echo 'true';
}
if($i == 0) {
echo 'true';
}
if($i == NULL) {
echo 'true';
}
All three statements will echo out. So if you want to know if $i is actually NULL or false as opposed to 0 (and some functions will return both) you have to use a different operator. === is used to see if the two are equivalent and not simply equal.
$i = 0;
if($i == NULL) {
echo 'true'; //This will succeed
}
if($i === NULL) {
echo 'true'; //This will NOT succeed
}