user9871124
user9871124

Reputation:

Issue comparing values in php

I have this php code meant to compare two values,a variable $rate received from a form which can either have values 'applaud' or 'boo' so I want to check if the value is neither of it and kill the page with an error message.I've tried that but ...localhost can not handle this request

HERE IS MY CODE:

<?php 
$rate=$_POST['rate'];
echo $rate;

?>

<?php

if($rate != 'applaud' OR $rate != 'boo')
{

die("Sorry there was a problem var rate was not well stated");
}

else
{
    echo 'yay ,well stated!!!';
}
?>

Upvotes: 1

Views: 43

Answers (2)

Zain Farooq
Zain Farooq

Reputation: 2964

Replace OR with && and best way to compare two strings in php is strcmp()

if(strcmp($rate,"applaud")!=0 && strcmp($rate,"boo")!=0)
 {
    die("Sorry there was a problem var rate was not well stated");
 }

If two strings are equal then it will return 0. If you want to learn more about strcmp() then you can visit here

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

I will apply some useful check to do so:-

<?php

if(!empty($_POST['rate'])){ //check data is coming or not actually

    $rate= $_POST['rate'];
    if($rate != 'applaud' && $rate != 'boo'){ // use && to check for neither of it
        die("Sorry there was a problem var rate was not well stated");
    }else{
        echo 'yay ,well stated!!!';
    }
}else{
   die("POST data missing!");
}
?>

Upvotes: 1

Related Questions