I'm trying to compare a POST variable with a string. Can someone help me see what in my PHP code is not written correctly? I've tried both '==' and '==='. Thank you for your help.
$action = mysqli_real_escape_string($mysqli, $_POST['action']);
if(strcmp($action, "save") == 0){
//do stuff
}elseif(strcmp($action, "load") == 0){
//do other stuff
}else{
//do even more stuff
}
why not simply use
if ($_POST['action']=='save'){
}elseif($_POST['action']=='load'){
}
don't understand the mysql in this contenxt
Don't know why you want to do this, but try casting $aciton, like (string)$action.
== is used to see if the two sides of comparison are equal, while === is used to check to see if they're identical meaning they are equal AND of the same type.
As for your code, you should just be able to do
if($action == 'save'){
echo 'save';
}
elseif ($action == 'load'){
echo 'load';
}
else{
echo 'none';
}
Related
I know that is basic question, but i can't figure out how to test if $_GET['zanr'] contains '' or 'sve' to do other stuff in my script...i try using this code but it only checks '' and 'sve' is ignored...so how to check if 'zanr' contains '' or 'sve' do stuff a else do stuff b?
if (isset($_GET['zanr']) === '' || isset($_GET['zanr']) === 'sve'){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
I also try using ?? but no success...thanks.
Since your goal is to check if your $_GET is empty, use PHP built in function: empty(). And your second statement is wrong because isset() is returning a boolean and therefore you're not validating the string itself. So be sure to remove isset() and just compare if $_GET['zanr'] has your specific string.
Use this:
if (empty($_GET['zanr']) || $_GET['zanr'] == 'sve'){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
Try below code.
if (isset($_GET['zanr']) && ($_GET['zanr'] == '' || $_GET['zanr'] == 'sve')){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
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!!!';
}
?>
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!");
}
?>
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
I want to check if either of these checkboxes are checked and if either of them are i want the new assigned variable $open_monday to = "yes else "no".
if (isset($_POST['open_monday_lunch'] or $_POST['opening_monday1'])) {
$open_monday = "yes";
}
else { $open_monday = "no"; }
Is that the right way to do it? I have never used or before. I just get a blank pag when trying to run it as if the syntax is incorrect.
Due to Operator precedence its always preferable to use && instead of and ,similarly || instead of or.please refer this link for additional info,
so try like this,
if (isset($_POST['open_monday_lunch'] ) || isset( $_POST['opening_monday1'])) {
$open_monday = "yes";
}
else { $open_monday = "no"; }
You can check it with -
if (!empty($_POST['open_monday_lunch']) || !empty($_POST['opening_monday1'])) {
I have submitted some code to the redirected url and now trying to use this to echo some information out but can't figure out where I am going wrong.
I have the following code:
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt) == '1'{
return 'failed';
}
?>
all I want to do is if the url has $login_attempt=1 I want to return the message 'failed' to the page.
There is no point of escaping anything if it doesn't enter anywhere important (like a database).
<?php
if ($_GET['login_attempt'] == '1') {
echo 'failed';
}
?>
Also, you have a problem in your if statement, that's corrected in the code above. Be sure to include all of the condition inside of parenthesis, and not just one side of the equality check.
Also, if you wish to display something on the screen, you should echo it, not return.
how about:
if ($login_attempt == '1'){
echo 'failed';
}
Try this one. Your error in $login_attempt == '1':
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1'){
echo 'failed';
return false;
}
?>
As others already mentioned you have several problems but the syntax error comes from this:
if ($login_attempt) == '1'{
it should be
if ($login_attempt == '1') {
Dont u think if ($login_attempt) == '1' should be something like this ($login_attempt == '1') Sorry...many others also suggested this :P
At the first, I must tell you that you have a mistake in your IF condition. You typed == outside of ().
In addition, you have to be aware of status of setting your variable through your URL. Check the code below. In this code, I made a function to check the status. Default status is true, and we will check it just for a negative condition. I hope it could be useful for you:
<?php
function check() {
if (isset($_GET['login_attempt'])) {
$login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1') {
return false;
} else {
return true;
}
} else {
return true;
}
}
if (!check()) echo('Error Message');
?>
Yes, this is just a question i would like to get an answer on. I experienced it a couple of times, where this:
if(!$one == $two){ echo "Not the same"; }else{ echo "The same"; }
Will not work, and
if($one == $two){ echo "The same"; }else{ echo "Not the same"; }
will work.
Why doesn't it work sometimes? I always need to recode like the second, when the first doesn't work.
! is having higher precedence than == so you should use parenthesis as:
if(!($one == $two))
You need to write
if(!($one == $two))
or
if($one != $two)
since the ! operator has a higher precedence than the == operator.
See also: http://www.php.net/manual/en/language.operators.precedence.php
You need
if(!($one == $two))
This is because without the brackets, it is checking if $one is false and then checking if $two == $one. The following is the only time that it will work without the brackets. Evaluating to if (true == true) as !$one = true.
$one = false;
$two = true;
if (!$one == $two)
{
echo "different";
}