PHP - If statement always evaluating as true? [closed] - php

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
}

Related

Check for null in an if loop [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 4 years ago.
Improve this question
I am having some problems with my program.
I am creating a script which displays all the properties in the properties table in our database and one row in the table should show available or sold. it should check if the puserId is null or 1 or more
//Execute the Query
$records = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($records)){
echo "<tr>";
echo "<td>".$row['propertyId']."</td>";
echo "<td>".$row['propNum']."</td>";
echo "<td>".$row['AptNum']."</td>";
echo "<td>".$row['street']."</td>";
if (empty(['puserId'])) {
$status = 'Available';
}else {
$status = 'Sold';
}
echo "<td>".$status."</td>";
When i use this it shows that all properties are sold, also the ones wich have a puserId of null. It doesn't give me any errors though.
Anybody knows how I should do this?
Thanks in advance :)
Replace:
if (empty(['puserId'])) {
with:
if (empty($row['puserId'])) {
in order to fix the missing variable $row typo (your actual code checks whether the array ['puserId'] is null, which is obviously false) . If your field is either null or numeric, you can also write that as:
if (is_null($row['puserId'])) {
but my advice is to avoid using empty/null values in table fields related to identifiers. Just give them a default value of 0 to make everything easier. At that point you could write your check as:
if ($row['puserId'] == 0) {
Other than the $row['puserId'] typo, empty doesn't really look useful there. It includes an isset check that is pointless considering you know that column exists. You aren't checking that any of the other columns are set before you use them. There's really no need to check that one either. Just evaluate it directly. null or 0 values will evaluate as false in the if condition..
if ($row['puserId']) {
$status = 'Sold';
} else {
$status = 'Available';
}

The statement "if this OR that do X" won't work in PHP [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 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';
}

$_GET["id"] don't respond [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 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

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.

Order in conditional statements [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php false place in condition
I have noticed that a lot of PHP code uses conditional statements like CONST == VARIABLE. I grew up with the syntax always articulated in reverse. Is there a reason for this structure?
Example:
// this is the way I see it most typically represented in PHP
if ( false == $foobar ) { // do this }
// this is the way I normally do it
if ( $foobar == false ) { // do this }
This is to prevent a common typo between == and =, known as a yoda condition. Consider the following:
if( false = $foobar) {
This would result in an error, catching what would be considered a bug, since you cannot assign anything to false. On the contrary:
if( $foobar = false) {
This is valid syntax, and is quite an easy mistake to make.
However, I typically prefer the if( $foobar == false) syntax, as unit tests should be able to catch these programmatic mistakes.
The way you normally do it is exactly how most programmers do it. The first example is called a yoda condition:
http://www.dodgycoder.net/2011/11/yoda-conditions-pokemon-exception.html
and is not the norm.

Categories