OR and && - logical operator php [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
What i'm doing wrong in this script?
if (!is_admin() or !is_product_category() && is_array($arrpags2))
I don't understand why don't work

If you are trying to check these !is_admin() or !is_product_category() together with OR operator than you can use like that:
if ((!is_admin() OR !is_product_category()) && is_array($arrpags2))
If you are trying to check these !is_product_category() && is_array($arrpags2) together with && operator than you can use like that:
if (!is_admin() OR (!is_product_category() && is_array($arrpags2)))

With no parenthesis, PHP will evaluate each expression from left to right
you should use parenthesis like this:
if
(
(!is_admin() || !is_product_category()) &&
is_array($arrpags2)
)
see this article about Operator Precedence

The && and || operators are intended for Boolean conditions, whereas and and or are intended for control flow.
for more detail see here

Related

working with nested if query [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I have a field which is supposed to display 1 of 3 icons based on a 2 conditions.
The key field here is VisitPlanRequired, if the answer to this is a "No" then the the icon should be na.png, however if it a "Yes", it then depends on another variable.
So if VisitPlanRequired = yes, then the two options are either ok.jpg or notok.jpg, the actual decider is whether the field VisitPlanIssued is null or contains a date, so if it contains a date, it should go to "ok", if it doesnt, it should go to notok.
This is the code I have so far, but I am struggling to get it to work for all three conditions, I would appreciate your help:
if ($data["VisitPlanRequired"]==='No')
$value="<img src=images/na.png id='image'>";
elseif
($data["AuditPlanIssued"])
{ $value="<img src=images/ok.jpg id='image'>";}
else
$value="<img src=images/notok.jpg id='image'>";
Something like this (really the basics of programming actually...
if ($data["VisitPlanRequired"] == 'No'){
$value="<img src=images/na.png id='image'>";
}else{
if( !empty($data["AuditPlanIssued"]) ){
$value="<img src=images/ok.jpg id='image'>";
}else{
$value="<img src=images/notok.jpg id='image'>";
}
}
Success

Ternary Operators and exit statements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey everyone I have this one line of code, and I was thinking about something, is this considered a legal assignment of a variable or will it cause errors. Furthermore is it okay to use exit() statements like this, or am I just terrible at coding somedays? Also if there is a duplicate question like this, please point me in the right direction that would be fanastic!
list($foo, $bar) ? generateValues($data) : exit("Unable to obtain useful information);
The list() you are using will assign $foo and $bar values if you use it like so:
list($foo,$bar) = array('fooValue', 'barValue');
so to properly use it in a tertiary statement would be like so:
list($foo, $bar) = (conditional) ? generateValues($data) : exit('...');
the exit will fire if the conditional is false, otherwise the array generated by generateValues() will be returned by the assignment, and list() will assign the values respectively.
Documentation.

PHP help needed with if statement and or [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have this if statement that I cannot figure it out:
<?php if($isFriend = true || $isOwner = true){echo $height;}else{/* do nothing*/}?>
the above code should be doing if is friend and or is owner then display height else display nothing.
I don't see why its wrong and its not doing what I want.
The problem is you're using assignments instead of comparisons:
$isFriend = true;
is an assignment
$isFriend == true;
is a comparison. But with values that are already booleans, you really don't need to compare them with true.
if ($isFriend || $isOwner) …
would be fine.
you have to use == instead of =
== equal will check equality while singe = is used to assign value to a variable.
So you need to change to:
<?php if($isFriend == true || $isOwner == true){echo $height;}else{/* do nothing*/}?>
You are assigning instead of comparing.
But don't use == please use ===
because in php the == operator is coercive.
Also in general it is bad practice to write
if ($condition === true) {...}
prefer
if ($condition) {...}
Try this:
<?php if($isFriend || $isOwner){echo $height;}else{/* do nothing*/}?>
No need to check if value of a variable is true or not just put it as an condition.
Now the comparison you were trying you need to usee == to compare between any two things example for your code.
<?php if($isFriend == true || $isOwner == true){echo $height;}else{/* do nothing*/}?>
NOTE: It is highly recommended to use=== instead of == to do stick checking and avoid type conversion.

How to determine the right number if "(" and ")" for a boolean IF statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Is this correct? Should it be made more simple somehow? Should there be more "(" and ")" on it, is this a safe 'if' statement for all situations?
if (empty($depart_raw) || empty($arrival_raw)) {
return "";
}
I ask because I get confused with how to know how many "(" and ")" to surround these kind of statements. To be clear, the above is attempting to check to see if either of the two variables are empty then to return a blank for the function.
The if statement requires one set of parentheses:
if (...)
Whether any ... expression within that requires parentheses or not depends on what the expression is. empty is a function language construct which requires its own set of parentheses as part of the function call:
empty($a)
Two expressions joined by an operator do not require parentheses; this is just fine:
$a || $b
empty($a) || empty($b)
If you have multiple operators, you may need to influence their precedence grouping using parentheses, just like in math:
(1 + 2) * 3
1 + (2 * 3)
($a || $b) && $c
$a || ($b && $c)
Adjust as needed.

Php condition doesn't work for html [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I currently want to display a link only on a certain page. The architecture on the website itself is over the index.php?menu=<pagename>. So I thought I could simply use:
<?php
echo $_GET["menu"];
if($_GET["menu"] = "mMenu-Point") :
?>
Test
<?php
endif;
?>
to execute the htm-code. However it seems like the code will be executed regardless what the value of the "menu" is. I tested this through the echo which strangely gets me the correct values.
I hope you can explain me why this doesn't work.
Thank you very much for your time and efforts!
try
if($_GET["menu"] == "mMenu-Point") :
At the moment you are setting (=) $_GET["menu"] not comparing it (==)
The assignment operator (=) is used to assign a value to a variable, element of an array, or property of an object
The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions.
You're using the assignment operator = here. You need
if($_GET["menu"] == "mMenu-Point") :
Hope this helps!
Your using the assignment operator (which is one equals) You need to use the comparison operator (==) or even (=== For an exact match)
http://php.net/manual/en/language.operators.comparison.php
So you need to be using:
if ($_GET["menu"]) == "mMenu-Point") :
For comparing to strings, I would recommend using the exact match operator ===
Double "=" :
if($_GET["menu"] == "mMenu-Point")
if($_GET["menu"] == "mMenu-Point") :
tow "=" pls

Categories