Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i came across a php statement and was thinking, is
if(isset($_SESSION['id'])==1){
}
very similar to
if (isset($_SESSION['id']) && $_SESSION['id']==1){
}
Let us break it down.
isset($_SESSION['id']) == 1
The first part, the isset() call, will return a boolean value (true or false), depending on if the session id is set or not.
When you compare an integer value with a boolean value, using the == operator, the integer will be coerced (or cast/type-juggled) into a boolean value. So, if the integer is a "truthy" value, i.e. greater than or equal to 1, then it will be turned into true. If it is lower than or equal to 0 then it will be turned into false.
In other words, if the session is set then the expression will be turned into true === true (which evaluates to true). If it is not set then it will be turned into false === true (which evaluates to false).
So, in conclusion, it probably does not do what you thought it did. Use the latter if statement.
The statements are not equivalent. Because isset() returns either TRUE or FALSE, the first test:
if(isset($_SESSION['id'])==1){
}
is exactly equivalent to:
if(isset($_SESSION['id'])){
}
because php converts 1 to the Boolean value TRUE so the == operator has compatible types. The second, on the other hand, not only test whether $_SESSION['id'] is set, but also, if it is, whether the value is set to 1 (after type juggling to resolve comparing a string with an integer).
Related
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
By the way i surprised about this
What will this code output and why?
$x = true and false;
var_dump($x);
Surprisingly to many, the above code will output bool(true) seeming to imply that the and operator is behaving instead as an or.
The issue here is that the = operator takes precedence over the and operator in order of operations, so the statement $x = true and false ends up being functionally equivalent to:
$x = true; // sets $x equal to true
true and false; // results in false, but has no affect on anything
This is, incidentally, a great example of why using parentheses to clearly specify your intent is generally a good practice, in any language. For example, if the above statement $x = true and false were replaced with $x = (true and false), then $x would be set to false as expected.
As explained here, the = operator has higher precedence than the and operator, causing $x = true to evaluate before true and false does, meaning that $x will take the value of true.
This will give you what you want:
$x = (true and false);
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What is the difference between if(isset($a)) and if($a) or if_exist($a) and if($a) in php?
With $a = false;:
if ($a) {} will return false, whereas if (isset($a)) {} will return true.
I do not know that if_exist you speak of. :)
Edit: Please check #Utkanos's answer for an excellent and more expansive explanation. :)
if (isset($var))
checks that a variable has been set and that it has a non-null value.
if ($var)
assumes the variable has been set and checks instead for it having a truthy value.
PHP has no function if_exist. Perhaps you're thinking of array_key_exists, which returns true if an array contains a certain key.
isset($var) checks wether the variable $var is not of type null and returns a boolean true or false.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
if($var) does an implicit type conversion to convert the $var from its original type to boolean which means it checks for truthy or falsy values.
To check wether a variable is truthy you can explicit convert it to a boolean:
$var = "foo";
var_dump((bool)$var); // outputs 'bool(true)'
$var = "0";
var_dump((bool)$var); // outputs 'bool(false)'
For a full list of how PHP handles the diffrent variable types see:
http://www.php.net/manual/en/types.comparisons.php
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
Is this if statement correct?
if ($row->totMED="0" or $row->MEDC="0"){
$avgMed='N/A';
}
else {
$avgMed='Medical: $'.($row->totMED / $row->MEDC);
}
You are looking for: $row->totMED == "0" or $row->totMED === "0"
Loose equality
== is loose equality, meaning that the compared values are similar in value. For example, all of these statements are true:
0 == false //true because 0 is like nothing
"" == false //true because an empty string is like nothing
1 == true //true because 1 is something
"abc" == true might be true depending on...things. In php it's true at the moment and in javascript it isn't. That's the problem with loose equality. The checking process can be complicated and the result can be unexpected. Strict equality is good.
Strict equality
===, or strict equality, means same in value AND type. All of these are true:
1 === 1
true === true
'abc' === 'abc'
and these are false:
1 === "1" // first value is integer and second is a string
true === "true" //first value is a boolean and second is a string
Basic assignment operator
A single = is an assignment operator, which sets the variable on the left to the value on the right. When you use =, you're setting the value of the variable, not comparing two values.
$row->totMED = "0" means that $row->totMED now has a value of "0".
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the difference between returning 1,-1 and 0 in PHP? What do they signify? What I want to know is that, is 'return 1' referring to true or false? And similarly about 'return -1' and 'return 0'.
I suspect you're asking about comparison functions use with usort(), as that's the only place where it's typical to return 1, -1, or 0.
In this case, 1 means that the first value is greater than the second, -1 means the first value is less than the second, and 0 means the values are equal. Although usort() doesn't actually require you to return these specific values -- all it cares about is whether the value is positive, negative, or zero. This allows you to write a comparison function like:
function compare_f($a, $b) {
return f($a) - f($b);
}
If you're asking about true/false contexts, see http://php.net/manual/en/language.types.boolean.php. It says:
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
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
It's not about PHP. It is about shell-conventions. 0 is a convention for "everything is fine" other numbers mean that something is wrong, but exact meaning is application-specific
So, if your script returns to shell with exit(0); the caller knows that it was successful, etc.
I am guessing that the answer you are looking for is that these values are "Truthy" and "Falsey" values.
They are not booleans as true and false are. However, evaluating them will equate to either true or false values for the purposes of if() or while() statements.
Here is some more reading on true, false, truthy, and falsey:
http://php.net/manual/en/types.comparisons.php
PHP will evaluate 0 as false and 1 as true - Meaning it behaves like a Boolean.
if (1) echo 'true';
if (0) echo 'false';
// result: true
With that said, if(-1) will also return true as would if(1000) so as it has already been stated it really does depend on context.