How to check if string is a positive integer in PHP

3 Answers

0 votes
$str = "25";

$n = (int)$str;

echo $n > 0 ? "yes" : "no";




/*
run:

yes

*/

 



answered Jun 25, 2022 by avibootz
0 votes
$str = "25";

$n = (int)$str;

if (is_int($n) && $n > 0 ) {
    echo "yes";
} else {
    echo "no";
}




/*
run:

yes

*/

 



answered Jun 25, 2022 by avibootz
0 votes
$str = "25";

if (ctype_digit($str) && (int)$str > 0 ) {
    echo "yes";
} else {
    echo "no";
}




/*
run:

yes

*/

 



answered Jun 25, 2022 by avibootz

Related questions

1 answer 272 views
2 answers 335 views
3 answers 322 views
1 answer 215 views
1 answer 189 views
1 answer 193 views
...