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.
Related
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 5 years ago.
Improve this question
I'm trying to show some information on screen only if a certain field is not null or if it doesn't say the word "null".
I'm working with a PHP script.
For some reason I have a database that has some values set as null as a string, instead as a real NULL value. As there are many items and I still don't know what causes to set the field as "null" instead of NULL, it's going to be easier to just set that with an if statement for the time being.
But for some reason, this is not working:
if ($row['cronograma'] != NULL || $row['cronograma'] != 'null') {
echo 'This is my reply';
}
If the field is actually NULL, the echo won't show up. If the field has the string null in it it does show up.
Please note that the fields in question are either NULL or has the "null" string without any spaces.
It looks like you're after and rather than or. That is, if the value is not null AND is not the string "null":
if ($row['cronograma'] != NULL && $row['cronograma'] != 'null') {
// ^^
You are looking for "and" rather than "or" logic. Also, you should always use strict equality (triple equals) when comparing with NULL to prevent weird edge cases from popping up:
if ($row['cronograma'] !== NULL && $row['cronograma'] !== 'null') {
echo 'This is my reply';
}
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
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 7 years ago.
Improve this question
So I can't get my head around this if statement.
What I am trying to do is get the person logged in from $_SESSION["user_name"] and if it matches one of two possible "admin" users, to give extra functionality. But it's always firing true.
For the example I'm going to have three users: Emma, John and Robert. Emma and John should be allowed the extra "admin functionality", but Robert is not allowed it. So...
My statement is:
if ($_SESSION["user_name"] === "emma" or "john"){
//give extra functionality as well as basic functionality
} else {
//give just basic functionality
But this is always firing true, so when I log in as my test account with the username Robert, he is also getting the extra functionality.
I've tried changing the quotation marks " of $_SESSION to apostrophes ', and also tried changing the operator from === to ==. I even tried = but found the hard way that this was setting my $_SESSION variable to "emma".
What am I doing wrong because I just can't seem to get my head around it?
If it's worth noting, this if statement is contained in a parent if statement that uses colons : and endif rather than brackets {}. The parent if statement is purely there to decide on what functionality to output based a column returned being empty or a user's name in there.
You need to redeclare the full condition:
if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john"){
//give extra functionality as well as basic functionality
} else {
...
}
Update With Explanation:
The pseudo code syntax for if statements is:
if ([some condition] [and/or] [another condition]) {
// then do some stuff
}
each of those [condition] statements is evaluated as either "truthy" or "falsey". So your original question could be re-written something like this:
if ([$_SESSION["user_name"] is equal to "emma"] or ["john" is not false, 0, or null]) {
// we will always get in here
}
Since "john" will always be "truthy" (it is not false, null, 0) it will always pass the condition.
The term "john" as an expression is true:
if ( "john" )
will evaluate to true and not throwing any syntax errors.
What you want is:
if ($_SESSION["user_name"] === "emma" || $_SESSION["user_name"] === "john") {
It may help to break this into steps. PHP will evaluate everything to the left of or to see if it's true or false. Then it will evaluate what's on the right of the or.
Turns out, "john" evaluates to true.
You're looking for:
if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john") {
Try something like this
if (($_SESSION["user_name"] === "emma") || ($_SESSION["user_name"] === "john"))
{
//do something
}
else
{
//other thing
}
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 7 years ago.
Improve this question
I am a beginner in php , and i want make a test with GET .
I want display , for example "ok" on my php page if my id parameter is 1 but i have always 1 when i change the id parameter to another value .
Details :
when i make this url :
http://localhost:81/test/testajax.php?id=2
expected result :
not ok
obtained result :
ok
testajax.php
<?php
if($_GET["id"] = 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
One equal sign (=) sets the value of a variable. $foo = "bar"; would set $foo to store bar.
You want to use two equal signs (==), which is a comparison operator. ($foo == "bar") would check to see if $foo is equal to bar.
You can check the different types of operators at http://php.net/manual/en/language.operators.php
May be you should go through this basics before you start.
you should put two equal signs to compare.
if($_GET["id"]==1)
correct code:
<?php
if($_GET["id"] == 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
As you are setting the variable, the if statement is always equating to true
Just thought it was worth Noting this as that is the logical reason for your issue
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 8 years ago.
Improve this question
if( strpos($_SERVER['REQUEST_URI'], '/') === TRUE) doesn't work.
What I am trying to is achieve that when on the homepage e.g. http://www.amazon.co.uk/ that it will trigger what is inside the statement.
This may be complicated by the fact this is a redirect from another page
var_dump($_SERVER['REQUEST_URI']); = /string(1)
echo $_SERVER['REQUEST_URI']; = "/"
php version 5.4
Thank you for your help, I have fixed my issue
That is because, like the docs tell you, strpos() never returns true.
It either returns a positive integer (found) or boolean false (not found).
So check for !== false.
The strpos() function, returns a numeric value representing the position of the second string in the first string. Hence it will never be equal to TRUE.
strpos() can return FALSE if the string isn't found, hence you should be writing
if( strpos($_SERVER['REQUEST_URI'], '/') !== FALSE)
strpos() from manual:
strpos — Find the position of the first occurrence of a substring in a
string
So you are checking if the return is TRUE, and it never is. You need to check it with !== FALSE. Why? Because === checks for the value AND the type of variable. When you do == it will check only for value, and when value is 0 PHP will cast it to FALSE, since PHP is loosely typed.