If statement, comparing values with = [closed] - 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".

Related

Are this if statements equal? [closed]

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

if( strpos($_SERVER['REQUEST_URI'], '/') === TRUE) doesn't work [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 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.

What is the difference between if(isset($a)) and if($a) in php? [closed]

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

Does PHP load the code inside a isset even if its not set? [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 8 years ago.
Improve this question
I have a performance question.
I structured a page in 4 different isset($_GET[""])
Lets call them
Menu1
Menu2
Menu3
Menu4
My question is, when the page is set to, example Menu1 (index.php?Menu1), is PHP still loading the code for Menu2, 3 and 4 even though they aren't set?
If it does I guess this is a very bad structure that will slow down the website if it gets alot of traffic since in some of those Menu's I use mysql.
Thanks in advance!
If you say if(isset($stuff)), the code inside the conditional will only be run if $stuff is set
If you have if(isset($_GET['menu1']), and go to index.php?menu1=something,
All the code inside other if's will never be run. Only the conditions would be checked.
The isset will only evaluate variables, no functions nor function return values are allowed. From the PHP Manual (isset):
<?php
$a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple'));
var_dump(isset($a['test'])); // TRUE
var_dump(isset($a['foo'])); // FALSE
var_dump(isset($a['hello'])); // FALSE
// The key 'hello' equals NULL so is considered unset
// If you want to check for NULL key values then try:
var_dump(array_key_exists('hello', $a)); // TRUE
// Checking deeper array values
var_dump(isset($a['pie']['a'])); // TRUE
var_dump(isset($a['pie']['b'])); // FALSE
var_dump(isset($a['cake']['a']['b'])); // FALSE
If you put multiple conditions in an if statement using the AND operator, the if statement will stop evaluating as soon as it find the first false condition. In the following example foo() will never be called:
$a = (false && foo());
$c = (false and foo());
In a similar way you can give the isset multiple parameters to evaluate. The manual says "If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered." :
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE
unset ($a);
var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE

PHP: difference between returning 1,-1 and 0? [closed]

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.

Categories