PHP executing header:location regardless IF statement parameters [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The 3 different equals
Can anyone tell me why, when using the code below, I am getting redirected to elephant.com rather than seeing a 'giraffe!
<?php
$foo="giraffe";
if($foo="elephant"){
header("location:http://www.elephant.com");
exit();
}else{
echo $foo;}
?>
Thanks for looking
J

if($foo="elephant")
You're assigning $foo here, rather than comparing it; you should be doing:
if($foo=="elephant")
The result of an assignment operation is the value that's just been assigned; in this case, 'elephant' is evaluating to true.

Your if() statement has a single equal sign. This doesn't do a comparison in PHP; it sets the value and returns true.
In order to do a comparison, you need to use either a double-equal or a triple-equal sign:
if($foo == "elephant") { .... }
or
if($foo === "elephant") { .... }
The difference between the two is that double-equal doesn't care about the variable's data type, whereas triple-equal does. In this case, there's not much difference between them, but it's worth learning and understanding the differences because they can bite you if you don't know them. More info here: http://php.net/manual/en/language.operators.comparison.php

Related

How does the if statement work with data returned from PHP methods? [duplicate]

This question already has answers here:
How exactly does if($variable) work? [duplicate]
(8 answers)
Closed 3 years ago.
Looking at this single if-statement, if a string is returned, then the condition of the if statement is false? and if null is returned the condition is true? That doesn't seem to make sense, though I know that, that is how this piece of code works. Does null always mean true? and does a stand alone string always get inturpruted as a false condition for an if statement?
BTW, this is just somthing ive been racking my head on, doesnt really matter, other than clarity sure would feel good.
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);}
tl;dr: See the table here, particularly the "Boolean" (far right) column.
A string will only evaluate to false if it contains zero characters (an empty string) or the single character "0" (zero), in which way it behaves like the integer zero. Otherwise, it will always evaluate to true.
Null will always evaluate to false. If it seems to be evaluating as true, there must be something else happening there.
If you want to be sure, you can use the identity operator === and/or its negative compliment !== or your own casting to be sure.
if ($conn->connect_error !== null) {
// …
}

PHP logical operator [duplicate]

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 4 years ago.
When the isset() is executed the following check $_SESSION['mat'] == "1" is executed or the second verification is directly skipped since it is the first false?
Is there is where I have the doubt?
if(isset($_SESSION['mat']) and $_SESSION['mat']=="1"){}
If the first part of your if statement already returns false the second part will not be evaluated. Your if statement looks good this way and shouldn't throw any index out of bounds errors.
When the isset is executed the following check is executed ($ _ SESSION ['mat'] == "1") or the second verification is directly skipped since it is the first false.
isset() will check if a variable is set otherwise it will return false. All the code inside the if() statement will be executed, so in your case, if the $_SESSION array variable isn't set, the second control, in your case and (that can be expressed also using &&) will be skipped.

PHP "Ternary Like" Boolean Expression Curiosity [duplicate]

This question already has answers here:
Short-circuit evaluation via the AND operator in PHP
(5 answers)
Closed 6 years ago.
This is just more of a "why does it work" and "why or why not use it" type question.
We all know PHP expressions using ternary operations
$var = (isset($i) ? true:false);
But in PHP something like the following works too. (It is not ternary, 3 parts, it is more of a binary operation, 2parts.)
$var = true;
isset($i) || $var = false;
Which may not be so practical :) but a more useful construction is
isset($i) || exit();
So the above (much better looking imo) would have the same result as
if(!isset($i)) exit();
But other than the common
defined('CONSTANT') || define('CONSTANT','FOO');
I rarely see this type of construct used in PHP. Why does this work? What is it called. Why is it not taught or used more. Are there cons to using it? And is there practical ways to use && in the same way?
This way of writing statements also exists in C, Perl and other languages, it plays with how the compiler evaluates statements chained with logical operators.
Imagine you have an expression if (a() or b()), then when a() returns true you don't have to evaluate b(), because the entire statement is true already. So b() is only called when a() is false. You can write this without the if, it still works the same way.
This shorthand is usually found like you described, to define default values or exit if a condition is not met.

Conditional in PHP [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.

What does "++" mean in PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've been learning PHP by reading all sorts of things on the web. I've trying to figure out what this is called so I can look it up and read about it and learn how it works. It's the "++" in the code I'm assuming counts up so that the codes knows when to do a new line. I not looking for corrections to this code I'm looking for what the "++" is called so I can look it up.
$i = 0
if (++$i == 10) {
echo "new line";
}
It's called the prefix increment unary operator. It increments the number or character, then returns its value.
The postfix version does pretty much the same but returns its value first, then increments the variable.
That is a pre-increment. So in that code, $i is first incremented (increased by one), and then its value is used to test for equality to 10.
So the code that you show essentially tests if 1 equals 10 (which it does not.)
It is a number operator named the prefix increment. It takes the previous stated number and adds 1 to it. You can find more information about arithmatic operators here: Howtoforge.com
It's the pre-increment operator. It will add one to the variable it precedes, and return the result. The post-increment operator returns the current value, and then adds one. The PHP Manuel has an article on all of the operators.
All PHP Operators
Incrementing/Decrementing Operators

Categories