I noticed PHP is_numeric()
accepts "E" as a number.
I have a string: "88205052E00
" and I want the result to be: NOT numeric.
Here is the code which I tested.
<?php
$notnumber = '88205052E00';
if(is_numeric($notnumber)) {
echo $notnumber . ' is a number';
} else {
echo $notnumber . ' is NOT a number';
}
?>
The Code above gives result:
88205052E00 is a number
How can I get the result to be: 88205052E00 is NOT a number?
The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.
The is_numeric() function in the PHP programming language is used to evaluate whether a value is a number or numeric string. Numeric strings contain any number of digits, optional signs such as + or -, an optional decimal, and an optional exponential. Therefore, +234.5e6 is a valid numeric string.
As the name suggests, numeric string is the string of numbers however not limited to string of 0-9. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus "+0123.45e6" is a valid numeric string value.
I will keep the answer incase it helps but as pointed out there are shortcomings with ctype_digit
in that it does not like -
or .
.
More likely then you want to use ctype_digit which checks if all of the characters in the provided string, text, are numerical.
Where as is_numeric — Finds whether a variable is a number or a numeric string
<?php
$s = "88205052E00";
if(ctype_digit($s)){
echo "Yes";
} else {
echo "No";
}
returns no.
Just use a regular expression:
<?php
if (preg_match("/^\-?[0-9]*\.?[0-9]+\z/", $notnumber)) {
echo "$notnumber is numeric\n";
} else {
echo "$notnumber is not numeric\n";
}
Results:
1234 is numeric
1234E56 is not numeric
-1234 is numeric
.1234 is numeric
-.1234 is numeric
-12.34 is numeric
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With