This question already has answers here:
In PHP, is there a short way to compare a variable to multiple values?
(5 answers)
Closed 7 years ago.
I have simple bit of code I want to to use to exit the script if a variable does not match 5 different values. I can only get it to work with one. the script keeps echoing "unknown request" even when the URL variable matches.
//Make sure the value of the variable matches one of the variables in the URL.
if($category !== ('food' || 'drink' || 'etc.'))
{echo "unknown request.";
exit();
}
thanks.
You have to compare each value. Also, you want AND &&:
if($category !== 'food' && $category !== 'drink' && $category !== 'etc.')
Or use an array:
if(!in_array($category, array('food', 'drink', 'etc.'))
Related
This question already has answers here:
How to check if a string is one of the known values?
(4 answers)
Closed 1 year ago.
I would like to check if variable is not 3 different numbers (ids), and if not one or other to display information.
For example :
if ($cat_id !== 151 || $cat_id !== 154 || $cat_id !== 160) {
echo 'something';
Fast and in my opinion easy to read solution:
if (!in_array($cat_id, [151,154,160], true))
{
echo "Something";
}
Edit: The third parameter i pass to in_array (true) means "strict type checking" if your $cat_id is not always an integer, you can also set this to false but i recommend to leave it on strict.
This question already has answers here:
In php, is 0 treated as empty?
(18 answers)
Closed 4 years ago.
I have the following error handlers in my PHP file:
if (empty($title) || empty($description) || empty($price)) {
header("Location: ../listing1.php?error=emptyfields");
exit();
} elseif (!is_numeric($price)) {
header("Location: ../listing1.php?error=onlynumbers");
exit();
}
But when I type in accordance with the is_numeric error handler it returns false telling it's error=emptyfields. I have tried switching positions but it still returns false and now I'm lost, though when I type in anything above 0 it returns true.
PHP's empty() function will consider the values 0 and "0" to be empty. Thus, even though the value is numeric and is properly set, it will still result in incorrect behavior.
You're going to need to be more careful about your first if condition. Instead of just empty($price), maybe consider doing (empty($price) && $price !== 0 && $price !== "0"). This is a much lengthier, more verbose validation, but it should eliminate the error.
This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I have a short shop form. You can buy there some pens, mugs etc.
After you put a number of items you want to buy I am trying to validate the input information and if it is not correct just change it into 0.
Looks like the !is_numeric function doesn't work, because it always makes the amount 0.
Any help please? Does ! work with this function at all?
$mugAmount = Input::get('mugAmount');
if(!isset($mugAmount) OR $mugAmount = NULL OR !is_numeric($mugAmount) OR $mugAmount < 0){
$mugAmount = 0;
};
It is because of this $mugAmount = NULL. This should be $mugAmount == NULL.
= and == means totally different things ;)
ps: besides you can remove this comparison to NULL because if it is NULL then !isset($mugAmount) will be true :)
This question already has answers here:
How to have multiple conditions on the same if statement? [duplicate]
(5 answers)
Closed 8 years ago.
hello I just wanted to check and see if this would be correct PHP syntax:
if ($input == "DeOnTRAY96#localhost"){
echo"
Projects: 1"?>
<br>
<?php
echo"
Admin: yes
";
}
elseif ($input == NULL){
die("Please enter password.");
}else{
header("Location:Invalidpassword.php");
exit;
}
Right where is says
if($input == "DeOnTRAY96#localhost"){
Could I put
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
And still have it work?
You don't want
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
You want
if($input == "DeOnTRAY96#localhost" or $input == "somethingelse"){
I might suggest using === instead of == in this case, as you'd like a type sensitive comparison.
Additionally, for $input == NULL you should use is_null($input). Null is weird in most programming languages, so language specific functions for testing are usually the way to go (rather than comparison)
OR syntax in PHP:
if($var == 'something' || $var == 'something else')
{
//do something
}
For reference:
|| means OR
&& means AND
For a more future-proof solution, consider in_array. I use it for as few as two options, if there's even the slightest chance there may be more added.
if( in_array($input, ["DeOnTRAY96#localhost", "somethingelse"]))
Once you get to four or more options, it's probably better to do something more like:
$whitelist = [
"DeOnTRAY96#localhost"
, "somethingelse"
, "anotheroption"
, "too many options to do inline!"
];
if( in_array($input, $whitelist))
This question already has answers here:
PHP if not equal(!=) and or (||) issue. Why doesnt this work?
(6 answers)
Closed 4 years ago.
Hope u people will be fine.
I’m working on a following code in which i want to use multiple php if conditions with multiple not operators (example is below), but when i execute following php code, it always returns true (mean content in always parenthesis always executed) even no condition is true.
I want to ask what is the problem in following code. Is there any specific syntax or rule for using multiple != operator in php conditions. And i am amazed to see that if i use following code by replacing != operator with == operator it is working fine.
if( $ext!="exe" || $ext!="html" || $ext!="htm" || $ext!="js" || $ext!="iso" || $ext!="zip" || $ext!="rar" )
{ // ececk extension
echo $ext."extension";
}
else{
echo "not match";
}
Waiting for your kind replies. and sorry for my bad english.
Better code:
$allowed = array('jpeg','png');
if(in_array($ext,$allowed)){
echo "Correct";
}
else {
echo "Wrong";
}
User XOR operator instead of OR as or checked all condition need to be identical while XOR return true if anyone from all is identical:
if(!($ext!=="exe") xor ($ext=="html")){ //Add more conditions
//each condition in new brackets & ! sign in start before all conditions
{ // ececk extension
echo $ext."extension";
}
else{
echo "not match";
}
Hope it will help