In this PHP tutorial, you shall learn how to check if the given string starts with a number using is_numeric() function, with example programs.
PHP – Check if String Starts with Number
To check if string starts with number, access the staring character of string and use PHP built-in function is_numeric() to check if this character is numeric or not.
The syntax of is_numeric() to check if given string starts with a number or not is
is_numeric($string[0])
You can use this condition in a PHP If statement, and have conditional execution when a string starts with a number or not.
Examples
1. Check if string starts with a number
In this example, we will take a string, say "4Hello World"
in $string, and check if this string starts with a number. We will use an If statement with the condition formed using is_numeric() function.
PHP Program
<?php
$string = "4Hello World";
if ( is_numeric($string[0]) ) {
echo "\"{$string}\" starts with a number.";
} else {
echo "\"{$string}\" does not start with a number.";
}
?>
Output
Conclusion
In this PHP Tutorial, we learned how to check if a string starts with a number, using PHP built-in function is_numeric().