Jump to content

Archived

This topic is now archived and is closed to further replies.

stevens

Easy IF Statement Help!

Recommended Posts

Hi there is something wrong with my if statement, please let me know what it is!! It doesnt echo denied! Im trying to only get accepted; if status doesnt = "test" and it is authorised.

[code]
$status = $_POST['status'];

if ((!strstr($response, "AUTHORISED"))||($status = "test")) {
    echo "denied";
}
else {
    echo "accepted";
}
[/code]

Share this post


Link to post
Share on other sites
Keep in mind that [b]=[/b] is an assignment operator what you want when checking values of a variable or array is a comparison operator which are [b]==[/b] and [b]===[/b] but [b]== [/b]being more commonly used for a normal string variable. This is a common error by those just starting out.

Share this post


Link to post
Share on other sites
The status = "test" works fine, but i will change it to == anyway. The problem seems to be with the brackets and || maybe?

Share this post


Link to post
Share on other sites
where is the variable $response getting its value from?

your if/then statment is checking the string value in $response to see if it contains the word "AUTHORISED".

Where in your code is $response populated?
Also, the word is spelled "AUTHORIZED" with a 'z'

Share this post


Link to post
Share on other sites
Not really because what is happening is that you are assigning the value of 'test' to the variable $status regardless of what it was prior to that statement. If the $_POST['status'] was "" or nothing when it gets to $status='test' then PHP will assign the value on the right to the variable on the left whereas $status =='text' will check the value of $status and if it is 'test' then it will return true otherwise it would return false if it were"" or nothing and fail. Here is an example to illustrate what I just stated

[code=php:0]<?php
$status = "UH OH";
if($status==""){//remove the second = sign and retest to see the result it will execute the else statement
  echo "$status contains no printable characters";
}
elseif($status=="UH OH"){
  echo"The proper value of $status is in the \$status variable!";
}
else{
  echo"The \$status variable is not correctly set.";
}
?> [/code]

Share this post


Link to post
Share on other sites
How can i make this return cost_check if they dont match?

This doesnt work:
[code]if (!$cost_check == $status) {
    echo "$cost_check";
}
else {
echo "ok";
     }
}[/code]

Share this post


Link to post
Share on other sites
[code]if ($cost_check != $status) {
    echo "error";
}else {
    echo "$cost_check";
}[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.