When I click y link,it is going to x.Why is that ?
x
y
<?php
if(isset($_REQUEST['hello']) == 'x')
{
echo 'x';
}
else if(isset($_REQUEST['hello']) == 'y'){
echo 'y';
}
else
{
echo "else";
}
try
if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) == 'x') )
The isset function returns either true or false and you are comparing that return value with strings 'x' and 'y'.
Since you are using == and not ===, true == 'x' will return ture.
To fix this first you need to check if the variable is set and only then compare it.
if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) === 'x'))
isset returns true or false, in both of these examples hello is set to something, so isset would return true (which does not equate to either x or y)
Hopefully this helps.
<?php
if(isset($_REQUEST['hello']) && $_REQUEST['hello']== 'x')
{
echo 'x';
}
else if(isset($_REQUEST['hello']) && $_REQUEST['hello'] == 'y'){
echo 'y';
}
else
{
echo "else";
}
?>
isset will check whether the request is set or not returns 0 or 1
x
y
<?php
if($_REQUEST['hello'] == 'x')
{
echo 'x';
}
else if($_REQUEST['hello'] == 'y'){
echo 'y';
}
else
{
echo "else";
}
<?php $myvar = $_REQUEST['hello'];
if($myvar == 'x')
{
echo 'x';
}
else if($myvar == 'y')
{
echo 'y';
}
else
{
echo 'else';
}
?>
Try this one
Related
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
**I try to Exit() First If Condition, continue to next IF Condition, But Cannot Continue next IF condition totally Exit Overall Please Fix My Problem Thankyou **
No need to use else condition if you want to run second if statement
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
What About the idea of this one.
<?php
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' )
{
if($var2 == 'run'){
echo 'run';
}
elseif($var3 == 'go'){
echo 'go';
}
elseif($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
}
just use only if.. use return
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}else {
return;
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}else {
return;
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}else {
return;
}
}
I got an array implode variable, $varString, that is setup to return 3 separate values listed below depending on the condition.
1
2
1,2
If ($varString == 1)
{
echo 'APPLE';}
ElseIf ($varString == 2)
{
echo 'BANANA';}
ElseIf ($varString == 1,2) //throws an error.
{
echo 'APPLE and BANANA';}
How do I do this for the case of 1,2?
I tried
ElseIf ($varString == '1,2') //throws an error.
{
echo 'APPLE and BANANA';}
ElseIf ($varString == "1,2") //throws an error.
{
echo 'APPLE and BANANA';}
As 1,2 could only be understood by PHP as a string, you should change your script to this:
If ($varString == '1')
{
echo 'APPLE';
}
ElseIf ($varString == '2')
{
echo 'BANANA';
}
ElseIf ($varString == '1,2') //no it doesn't
{
echo 'APPLE and BANANA';
}
and also, string should always be in ''
ElseIf ($varString == '1,2') { echo 'APPLE and BANANA';}
I have the following code:
if ($Type != "DEA" and $VA != "Allowed" and $VolSess != 1) {
$max_rows = max($CMSReg_num_rows);
if ($max_rows == 0) {
mail($to, $subject, $body);
header('Location: '.bloginfo('home_url').'/profile');
}
}
The problem I have is that that an email is sent despite the if-statement being false, and only an email is sent. The rest of the code is not executed, i.e. no redirect. And when I comment out the mail() function, it does not send the email.
And when I add this code:
if ($VA == "Allowed") {
echo "VA = " . $VA;
}
if ($VolSess == 1) {
echo "VolSess = " . $VolSess;
}
I get this output:
VA = Allowed VolSess = 1
So I know that the condition in the if statement is false.
AND has a different order of precedence compared to &&. So your expression does not evaluate as you expect it to.
("$Type" != "DEA" and $VA != "Allowed" and $VolSess != 1)
should be
(("$Type" != "DEA") and ($VA != "Allowed") and ($VolSess != 1))
or
("$Type" != "DEA" && $VA != "Allowed" && $VolSess != 1)
for it to work as you expect it. This is one of those tiny mistakes/bugs that's easy to overlook.
try do an else after...
elseif($VA == "Allowed"){}
Try using the WordPress wp_mail().
die; after header() and also add 302 as a second argument to the header() function.
Enable error reporting with ini_set('display_errors', true); error_reporting(-1); on top of your PHP code.
Tell us what you see after making these changes.
Try:
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = max($CMSReg_num_rows);
if ($max_rows === 0)
{
mail($to, $subject, $body);
header('Location: ' . bloginfo('home_url') . '/profile');
}
}
EDIT
The above works, but so does the oringal question code... The problem is elsewhere.
<?php
$Type = 'foo';
$VA = 'Allowed';
$VolSess = 1;
if ($Type != 'DEA' and $VA != 'Allowed' and $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Orig True';
}
}
else
{
echo 'fine?';
}
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Second True';
}
}
else
{
echo 'fine?';
}
?>
Both print 'fine?' Implying your error is elsewhere in your code.
I have a PHP script and MSSQL tables, I got the answer key in a variable stored in $right_answer and user selected answers in $user_answer_select THe format is something like this
5+10?
A) 10
B) 15
C) 20
D) 25
E) 50
Answer key: B
what I want to do is put a check mark next to B if its correct and a X if it is wrong, how would I make the if else statements here?
This is the code I currently have
if(($user_answer_select == $right_answer) && $user_answer_select == 'a') $a_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'b') $b_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'c') $c_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'd') $d_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'e') $e_sel = "<img src=\"tick_icon.gif\">";
This is wrong because some of the questions that don't have answers for are highlighted as true. What's the way to do this?
$answers = array ( "A"=>10, "B"=>15, "C"=>20, "D"=>25, "E"=>50 );
$right_answer = "B";
$user_selected_answer = "A";
echo "5+10?<br/>";
foreach ($answers as $key => $value) {
echo $key.") ".$value;
if ($value === $user_selected_answer) {
if ($value === $right_answer){ echo "check!"; }
else { echo "X"; }
}
echo "<br/>";
}
echo "Answer key: $right_answer";
if ( $user_answer_select == $right_answer ) {
$correct = true;
} else {
$correct = false;
}
Then in the correct answer on the form:
<?php echo $correct == true ? 'x' : ''; ?>
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;
}
}
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.
I always try to use the more restrictive === or !== when I absolutely positively need a boolean answer, so:
$success = 'true';
if( $success === 'false'){
...
}
Just in case.
true is a boolean, 'true' is a string.
Yes, there is a difference. Every value in a PHP variable (or almost any programming language) has a "type". When creating/assigning a value with quotes,
$foo = 'true';
you are creating a value whose type is a string, and when creating/assigning a value without quotes, you are creating a variable whose type is boolean
$bar = true;
Like some other modern, dynamic languages, PHP tries really hard to arrange things in such a way that you don't have to worry about things like type. For example, a lot of languages will NOT let you compare the equality of two variables if they aren't of the same type, so something like
if('true' == True) ...
isn't valid code in python (you'll get an exception). PHP, on the other hand, tries to be nice and (behind the scenes) says "well, if you use any string in an equality operation, we'll pretend the string is of type boolean and true, unless it's a zero-length string". That's why, 90% of the time, you can get away with doing either.
However, there are differences. To start with the pedantic, the computer that PHP is running on needs to set aside more memory for a string than it does for a boolean. In this day and age it's a trivial amount, but waste not/want not.
More importantly though, there are times where PHP's type coercion does weird things that make no sense. For example, consider the following
if ("false" == false) {
echo "true\n";
} else {
echo "false\n";
}
This statement will echo "false", even though intuitively you'd thing it would echo true, since "true" == true evaluates as true. There are a lot of edge cases like this where PHP will act in seemingly weird ways. So, in trying to make the general case simpler (let's convert variables for people), they made some less common cases more complex, which can lead to hard to track down bugs. Things get really gnarly when some people on your team understand the behind the scenes coercion, and others don't.
So, by and large, it's always best to return explicit booleans (no quotes) from methods and functions that are returning success. Experienced programmers expect it and inexperienced programmers will be baffled by some of the bugs that pop-up when strings are used instead.