I am working on a project and in that project I have two objectives
interger
or in case is an integer, if is less than 1So I wrote this code
<form action="" method="get">
<input type="text" name="quantity" value="2"/>
<input type="submit"/>
</form>
<?php
if (!is_int($_GET['quantity']) || $_GET['quantity'] < 1){
$_GET['quantity'] = 1;
}
echo $_GET['quantity'];
The problem, I am facing is that, the program always echo 1
even if the data is less than 1 or is not an integer at all.
Help me solve this problem please
Try replacing
if (!is_int($_GET['quantity']) || $_GET['quantity'] < 1){
with
$quantity = (int) $_GET['quantity'];
if ($quantity < 1) {...}
This casts it to an integer, and if not an integer, casts it as being assigned 0. Then it compares whether it is < 1 or not, and should have the desired result that you are looking for.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments