! ( NULL || 0 || '' ) if condition in PHP at the same time - php

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
}

Related

use OR in if codition to print reverse results in php

I am trying echo result "OK" if ( 1 either both ) variable true, i did so far like this
<?php
$user_id = $_SESSION['user_id'];
$point= "select points from users where id = $user_id "; // in db right now points = 2000
$flag= "select m_boost from users where id = $user_id "; // in db right now flag = 1
?>
<div class="box border">
<div class="box-title">
<?php
if($point < 1000 || $flag = 0) {
echo "not ok";
} else { ?>
echo "ok";
<?php }?>
</div>
it's working if i do like this
if(($point == '2000') || ($flag == '0') ){
but i don't want == operator for $point i want less than < $point < '999'
The problem is :
Keep getting result " Not Ok " even one variable (flag = 1) is true in db
Expected Results:
i want to print "OK" if $point > 1000 or flag == 1,
try this
when you using OR condition you should careful about condition and login. you should implement logic in if condition instead of else.
if($point > 1000 || $flag ==1) {
echo "ok";
}else {
echo 'Not ok'
}
or AS YOU WANT
if($point < 1000 ) {
echo "Not ok";
}else if($flag ==0) {
echo 'Not ok'
}esle {
echo 'ok'
}
or you can use this way
$a=false;
if($point < 1000 ) {
$a=true;
}else if($flag ==0) {
$a=true;
}esle {
$a=false;
}
// you can use this variable in your condition.
if($a) {
echo "ok";
}else {
echo 'Not ok'
}
When you are using OR, if the first condition is met the second is disregarded.
Also, make sure you use double equals (==) for comparison, not single equals (=) which means assignment.
Therefore you want to replace this:
if($point < 1000 || $flag = 0) {
With one of these:
SWAPPED AROUND
if($flag == 0 || $point < 1000) {
or
USING && INSTEAD
if($point < 1000 && $flag == 0) {
Depending on what behaviour you're looking for. It's a little unclear - so any additional clarification from you would be helpful.
Once you get something that you think is working, try to test all possible combinations so that you can be confident it works how you wish.
$flag = 0
You're setting the variable's value to 0.
Example:
$flag = 1;
if($a < $b || $flag = 0){ //$flag's value is 0 now.
...
...
}
In conditions comparisons, the right operator is "==".
if($point < 1000 || $flag == 0) {
List of comparison operators
but i don't want == operator for $point i want less than < $point <
'999'
Didn't fully understood your goals, but maybe:
For checking a value in a range the right logical operator should be "and" (&&);
if($point > 0 && $point < 999){
List of logical operators
Update:
if $point > 1000 or flag == 1
if($point > 1000 || $flag == 1){
echo "ok"
}
if (!$flag && $point < 1000)
{
echo "Not OK";
} else {
echo "OK";
}
Writing this into a truth-table:
flag point result
0 < 1000 Not OK
1 < 1000 OK
0 >=1000 OK
1 >=1000 OK
Worked for me now as per my question
i think we are all here for some contribution reason, flagging down a question is not a way, if you got solution than respond otherwise my question was 100% clear.

Using 'and' and 'or' in an if/else PHP statement

I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..

Logical Operators and Precedence

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.)

Weird PHP number issue

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

PHP creating a while loop that is dependent on multiple variables that are independent of one another?

I'm stumped on this and my searches aren't turning up anything relevant.. I need to do a while loop that will continue if either of 2 variables are true... as far as I can tell you can't do a "while ($var = '' and $var2 = ''); so I tried this, basically I figured I could just set 2 different if statements so that it would change the variable "continue" if it went past 4 iterations (if $i >= 4), however this just gives an infinite loop:
function whiletest () {
$i = 1;
do {
echo 'output';
if ($status != 'true') {
$continue = 1 ;
}
if ($i >= 4) {
$continue = 2 ;
}
$i++ ;
} while ($continue = 1 );
}
Are you looking for a construct like this:
while($var1 == 'value1' OR $var2 == 'value2') {
...
}
That will continue to run while either condition is true.
Why wouldn't the following work?
while (($condition1) || ($condition2)) {
// loop stuff
}
As long as the expression within the while parens is true, the loop will execute.
The while statement evaluates a boolean expression. You should be able to write out:
while( ($status != true) && ($continue == 1) ) {}
Also in your code (if its a c/p), you have $continue = 1. This will always evaluate to true.
EDIT:
while (($status) && ($i < 4))
As for the last while, it just looks like an infinite loop to me.
You shouldn't need the $continue variable. This should do the trick:
$i = 1;
do {
//do other stuff here (possibly changing the value of $status)
echo 'output';
$i++;
} while ($status != 'true' && $i < 4);
Keep in mind that this will always run the loop at least once. If $status might start out as 'true' and you want the loop to run zero times if it is, you want:
$i = 1;
while ($status != 'true' && $i < 4) {
//do other stuff here (possibly changing the value of $status)
echo 'output';
$i++;
}

Categories