What does $var = value actually return?
This seems like a very mysterious part of PHP to me, and I was wondering if someone could clarify because the manual doesn't seem to include this (or I just couldn't find it anywhere).
What would some of these things return?
if($c = mysql_connect($host, $user, $pass)){
echo 'Success';
}else{
echo 'Failure';
}
Woul开发者_如何学Cd this always echo 'Success' because $c is successfully assigned to true or false? I'm wondering if I can do this or if I have to define $c on the previous line.
Thanks.
It is mentioned in Assignment Operators:
The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is
3
.
So $c = ...
will result in true
if true
is assigned to $c
and in false
if false
is assigned.
That is also the reason why iterating over query results
while(($row = mysql_fetch_array(...)))
works.
When in doubt, build a test-case!
Build a test-case:
if ( $c = true ) {
echo 'Success';
} else {
echo 'No Success';
}
Run this online: http://codepad.org/jxylNzOu
You'll note that the first block will only be executed if the conditions turn up true
(or what PHP evaluates as true
when asked), so it's not asking if a value was successfully applied to $c
, but rather whether the value applied to $c
is true
.
Regarding your specific example...
If your case, where you're attempting to open a connection to MySQL, $c
will be true
if the connection is made, resulting in the first block of the if-statement
being ran. Otherwise, if no connection is made, $c
will be false, making the condition of the if-statement
false, resulting in the running of the else
block.
According to the documentation for mysql_connect()
, one of two things can be returned from this function.
- A MySQL Link Identifier (if a connection is made)
FALSE
, indicating a failure to make a connection.
So there's no difference between the following code examples
$conn = mysql_connect( $host, $user, $pass );
if ( $conn ) { /*...*/ }
And
if ( $conn = mysql_connect( $host, $user, $pass ) {
/*...*/
}
It does not always echo 'Success'. PHP first assigns the result of mysql_connect
to $to
then evaluates boolean value of $to
. But it's better to use this way to ensure the understandability:
$c = mysql_connect($host, $user, $pass)
if($to) {
echo 'Success';
} else{
echo 'Failure';
}
PHP is a "weakly typed" language which means php doesn't require (nor support it for that matter) explicit type declaration of variables.
Pay attention to consufe or evaluate 0 1 as true/false (boolean value)
Take this case as sample:
$s = "0"; //String s = '0'
$res = strstr($s,'0'); //Search the character zero into the string $s
if ($res){
echo "Zero found!";
}else{
echo "Zero not found!"
}
//Hey!! Whats up!!?? Zero is not found!
This is because zero, which is the returned value of the function strstr
, is evaluated as FALSE
producing in some cases unexpected results.
The correct way is to use the Not Identical
operator !==
where value and type is compared
The previous example should be:
$s = "0"; //String s = '0'
$res = strstr($s,'0'); //Search the character zero into the string $s
if ($res !== FALSE){//Check for value AND type
echo "Zero found!";
}else{
echo "Zero not found!"
}
//yeah now it works!
So in your case I would write the if statement as:
if(($c = mysql_connect($host, $user, $pass)) !== FALSE){
echo 'Success';
}else{
echo 'Failure';
}
精彩评论