This is because of PHP loose type comparison, you are comparing string with integer, so PHP internally will cast this string to integer, and all character in your string will be casted to 0. Show
First step to fix your code, will be to create an array of strings instead of integers:
This will fix your case, but will not work as you expect it to, since
To have this all logic in more compact form, use regular expresion, as posted by others before.
To check if string contains numbers, check if there is any character that is a digit. Iterate over the characters of the string, and for each character, use ctype_digit() function to check if the character is a digit or not. When you found a digit, you can break the loop and confirm that there is a number in the string. ctype_digit() function returns true if the string passed to it has digits for all of its characters, else it
returns false. In this tutorial, we will pass each character of the string, to ctype_digit() function, as a string. In this example, we will take a string, “apple123”, write a for loop to iterate over individual characters of the string, and for each character
check if it is a digit, using ctype_digit() function. PHP Program Output ConclusionIn this PHP Tutorial, we learned how to check if a string contains number(s) or not, using ctype_digit() built-in PHP function. Topic: PHP / MySQLPrev|Next Answer: Use the PHP strpos() FunctionYou can use the PHP The Let's check out an example to understand how this function basically works:
Related FAQHere are some more FAQ related to this topic:
In this guide, we will be showing you how you can check if a string is a number in PHP using is_numeric(). The is_numeric() function in PHP is incredibly straightforward to use and understand. This function gives you a simple way to check whether the passed in variable is a number, or numeric string. However, a string that contains a floating-point number that uses a comma (,) instead of a dot (.) will fail this test. You will find that the is_numeric() function is very useful when dealing with user input. For example, verifying that a user typed in a number into an input box. Over the next section, we will show you how to use the is_numeric() function within your PHP scripts. Definition of the is_numeric() Function in PHPLet us start by exploring how the is_numeric() function is defined within PHP. It is a rather simplistic function that only has a single parameter. The definition below shows you the function, the parameter, its expected variable type, and the variable type the function will return. The single parameter this function has takes in the value you want to check if it is numeric. In addition, this function will return a Boolean value (true or false). If the value is a number or a numeric
string, the function will return Now that we know the definition of the is_numeric() function within PHP let us explore how to put this function to use. To showcase how this works with different values, let us run through several different examples. Each of these examples will show you how this function works. The is_numeric() Function on a NumberFor this first example,
let us see the result of this function when it is used on a standard number. The expected result from this will be for PHP’s is_numeric() function to return We start this example by creating a variable called “ Next, we utilize the The var_dump() function will show us the value that is returned. After running this code snippet, you should have the following result. Since we passed in a number, we got back “ Checking if a Numeric String is a NumberFor the second example, we will be showing how PHP’s is_numeric() can check whether a string contains a number. Again, we pass in a value that we know will cause the
function to return The script is started with the creation of the “ We then use the The above code sample should end up printing the following value. Since the string we provided can be evaluated into a number. is_numeric() with a Non-NumberNow that we have shown how PHP’s is_numeric() function works on a numeric string and a number let us pass in an invalid value. We create a variable called “ Next, we use the “ Since our string
contains elements that PHP can’t evaluate into a number, this function will return is_numeric() Evaluates All Number TypesPHP’s is_numeric() function can evaluate more than just integer and floating-point numbers. For example, if your variable contains a hexadecimal number such as “ To showcase this, let us write a quick PHP
example to test how the is_numeric() function handles the number Start this PHP script by creating a variable called “ Like our previous examples, we use the “ Below you can see how the is_numeric() function evaluated our hexadecimal number as numeric. ConclusionIn this tutorial, we showed you how you could utilize the is_numeric() function in the PHP language. This function easily checks whether the given variable contains a number or a numeric string. If you have any questions about using this function, please comment below. To learn more check, out our numerous PHP tutorials, or if you want to learn a different language, browse our other coding guides. Weekly Updates Straight To Your Inbox Receive our Raspberry Pi projects, coding tutorials, Linux guides and more! How do I check if a string contains a number?To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.
How check string is value or not in PHP?The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.
How do you check if a string has numbers and letters?Check if a string is numeric, alphabetic, alphanumeric, or ASCII. Check if a string contains only decimal: str.isdecimal(). Check if a string contains only digits: str.isdigit(). Check if a string contains only numeric: str.isnumeric(). Check if a string contains only alphabetic: str.isalpha(). How do you check if a variable contains a value in PHP?PHP isset() Function
The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
|