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

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

Related

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

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.

Make a value into NULL instead of empty in php [duplicate]

This question already has answers here:
MySQL and PHP - insert NULL rather than empty string
(10 answers)
Closed 3 years ago.
I can't make the empty value to be checked as NULL in database instead of leaving an empty space when I am inserting a value.
I don't want to leave an empty/blank space for an entry when it has no value.
So if there is no file selected, make that entry into NULL
Is it unnecessary to be in NULL?
if (empty($_FILES['logo']['name'])) {
$code = NULL;
} else {
}
It's a normal problem with null-able strings. For a quick solution, you can change your code to:
$code = NULL;
if( !empty($_FILES['logo']['name']) ){
...
}
For more details, you can check this question:
MySQL and PHP - insert NULL rather than empty string
$logo = (!empty($_FILES['logo']['name']) ? "logo" : "NULL");
try this

Loop back to beginning of array when you reach the end [duplicate]

This question already has answers here:
Loop a multidimensional array and only print two specific column values per row
(6 answers)
Closed 6 years ago.
Okay, I Have an array and I want to loop through it every time you press a button. And when the array has made it to its last number it starts on 0 again.
So it starts on 0, when I press the button it goes to 1 and when I press again it goes to 2. Lets say it has a lenght of three, so when I press again it should go back to 0...
I just don't know I can do that. Can someone help out? It would be hugely appreciated!
This is what I tried so far:
$foo = array('bar', 'baz');
$foo = 0;
$foo++;
update($foo);
Ofcourse is this not working because $foo becomes 0 each time the page reloads...
Yes this is quite simple. Just have a conditional statement that sets your loop variable back to 0 if it hits the length of the array. Here is some very basic pseudo code.
if(loopvariable = arrayLength){
set loopVariable = 0
}
Okay, I solved it.
I just put the variables in an session.
This is my code:
if(!isset($_SESSION['b']))
$_SESSION['b'] = 0;
$_SESSION['b'] += 1;
if($_SESSION['b'] > 4){
$_SESSION['b'] = 0;
}

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

Store values in an array and pass it in url [duplicate]

This question already has answers here:
Passing arrays as url parameter
(11 answers)
Closed 9 years ago.
$deposit=$_POST['amountdeposit'];
$arr= array();
for($i=0;$i<10;$i++)
{
if($arr[$i]=='\0')
{ $arr[$i]= array("$deposit");
}
break;
}
$page= "step2.php?arr=$arr";
header("Location:$page");
?>
what i want to do is each time there's a change in $deposit , this value is stored in $arr[$i] and then it is passed in a url so that i could use GET on that step2.php page.
What I see is just arr=array instead of values :/ please help me.
You want http_query_string. It will do exactly what you want.
A couple of other comments have recommended http_query_string, however I would use serialize along with urlencode.
Replace:
$page= "step2.php?arr=$arr";
with:
$page= "step2.php?arr=" . urlencode(serialize($arr));
Then when you get to step2.php, unserialize(urldecode($_GET['arr'])) will contain your array as you originally built it.

Categories