Quick question. Is there a difference between
$success = true;
and
$success = 'true';
I know they are not '==
' to each other, but is there a difference in using them?
EDIT: I found that using '===' instead of '==' when seeing if $success is false solved my problem. My question now is that, should I just use strings in a case like below, and stick with '=='?
$User->ProcessLogin();
$loginsuccess = $User->ProcessLogin();
if ($loginsuccess == true) {
echo "<big>Success<big><br />";
echo "<p>We are now redirecting you to the member area.</p>";
echo "<meta http-equiv='refresh' content='=2;MyAccountNEW.php' />";
}
elseif ($loginsuccess == false) {
echo "<span class='sorry'><b>Sorry, your account could not be found.</span></b><div id='shopperlogin'> <img class='shopperlogintext' src='images/shopperlogin.png'>
<br />
<form method='post' action='loginNEW.php' name='loginform' id='loginform'>
<fieldset>
<label for='username'>Username:</label><input type='text' name='username' id='username' /><br />
<label for='password'>Password:</label><input type='password' name='password' id='password' /><br />
<input type='submit' name='login' id='login' value='Login' />
</fieldset>
</form></div>";
}
Here's part of the class..
function ProcessLogin() {
if (!empty($_POST['username']) && !empty($_POST['password'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
$this->loggedin = true;
$success = true;
}
else {
$success = false;
}
return $success;
}
}
-1 is considered true , like any other non-zero (whether negative or positive) number!
Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.
Value 0 and 1 is equal to false and true in php.
NULL essentially means a variable has no value assigned to it; false is a valid Boolean value, 0 is a valid integer value, and PHP has some fairly ugly conversions between 0 , "0" , "" , and false .
Any non-empty string evaluates to true and an empty string evaluates to false. The following script might shed some light for you:
<?php
if('true' == true) {
echo "'true' == true";
} else {
echo "'true' != true";
}
echo '<br />';
if('false' == true) {
echo "'false' == true";
} else {
echo "'false' != true";
}
echo '<br />';
if('foo' == true) {
echo "'foo' == true";
} else {
echo "'foo' != true";
}
echo '<br />';
if('false' == false) {
echo "'false' == false";
} else {
echo "'false' != false";
}
echo '<br />';
if('' == true) {
echo "'' == true";
} else {
echo "'' != true";
}
echo '<br />';
if('' == false) {
echo "'' == false";
} else {
echo "'' != false";
}
?>
Here is the output:
'true' == true
'false' == true
'foo' == true
'false' != false
'' != true
'' == false
As requested, here are some more examples comparing == with === for various values.
<?php
echo "<b>'true' vs. true</b><br />";
if('true' == true) {
echo "'true' == true<br />";
} else {
echo "'true' != true<br />";
}
if('true' === true) {
echo "'true' === true<br />";
} else {
echo "'true' !== true<br />";
}
echo "<br /><b>'false' vs. true</b><br />";
if('false' == true) {
echo "'false' == true<br />";
} else {
echo "'false' != true<br />";
}
if('false' === true) {
echo "'false' === true<br />";
} else {
echo "'false' !== true<br />";
}
echo "<br /><b>1 vs. true</b><br />";
if(1 == true) {
echo "1 == true<br />";
} else {
echo "1 != true<br />";
}
if(1 === true) {
echo "1 === true<br />";
} else {
echo "1 !== true<br />";
}
echo "<br /><b>0 vs. false</b><br />";
if(0 == false) {
echo "0 == false<br />";
} else {
echo "0 != false<br />";
}
if(0 === false) {
echo "0 === false<br />";
} else {
echo "0 !== false<br />";
}
echo "<br /><b>1 vs. 'true'</b><br />";
if(1 == 'true') {
echo "1 == 'true'<br />";
} else {
echo "1 != 'true'<br />";
}
if(1 === 'true') {
echo "1 === 'true'<br />";
} else {
echo "1 !== 'true'<br />";
}
echo "<br /><b>empty string '' vs. false</b><br />";
if('' == false) {
echo "'' == false<br />";
} else {
echo "'' != false<br />";
}
if('' === true) {
echo "'' === false<br />";
} else {
echo "'' !== false<br />";
}
?>
Output:
'true' vs. true
'true' == true
'true' !== true
'false' vs. true
'false' == true
'false' !== true
1 vs. true
1 == true
1 !== true
0 vs. false
0 == false
0 !== false
1 vs. 'true'
1 != 'true'
1 !== 'true'
empty string '' vs. false
'' == false
'' !== false
First is a boolean. 2nd is a string
You can see their difference with this.
$success = 'true';
$success2 = true;
var_dump($success);
var_dump($success2);
And also check out the result from this
var_dump($success == $success2);
var_dump($success === $success2);
You should also study this type comparison table. Real neat information and helps you understand PHP a bit more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With