PHP how to check if variable is not 3 different numbers [duplicate] - php

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.

Related

My error handlers return false when they shouldn't [duplicate]

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.

Is there a function like in_array that will check for empty values in PHP? [duplicate]

This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
It is known that in_array() function can be used to check if an array contains a certain value. However, when an array contains the value 0, an empty or null values pass the test, too.
For example
<?php
$testing = null;
$another_testing = 0;
if ( in_array($testing, [0,1,5,6,7]) )
echo "Found";
else
echo "Not Found";
echo "<br>";
if ( in_array($another_testing, [0,1,5,6,7]) )
echo "Found";
else
echo "Not Found";
?>
In both cases "Found" is printed. But I would like the first case to print "Not Found", and the second case - "Found".
I know I can solve the problem by adding an extra if statement, or by writing my own function, but I want to know if there are any built-ins in PHP that can perform the checks.
This behavior is explained by the fact that null == 0. But null !== 0. In other words, you should check for the types as well.
You don't need another function. Simply pass true as the third parameter:
in_array($testing, [0,1,5,6,7], true)
In this case in_array() will also check the types.

Checking if input is not numeric - !is_numeric [duplicate]

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 :)

PHP if (not multiple values) not working? [duplicate]

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.'))

PHP Syntax: "or" in "If" Statement [duplicate]

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))

Categories