How do you check if a string contains a number in php?

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.

First step to fix your code, will be to create an array of strings instead of integers:

$numbers = array_map(function($n){ return (string)$n; }, range(0,9));
for ($i = 0; $i <= strlen($string)-1; $i++) {
    $char = $string[$i];
    $message_keyword = in_array($char,$numbers)?'includes' : 'doesn\'t include';
}

This will fix your case, but will not work as you expect it to, since $message_keyword is overwritten on each loop, so will receive message for last character only. If you are aiming to only check if string contains number, you can stop checking after encountering first number:

$message_keyword = 'doesn\'t include';
for ($i = 0; $i <= strlen($string)-1; $i++) {
    $char = $string[$i];
    if(in_array($char, $numbers)) {
        $message_keyword = 'includes';
        break; //found, no need to check next
    }   
}

To have this all logic in more compact form, use regular expresion, as posted by others before.

PHP – Check if String contains Numbers

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.

Example – Check if String has Number(s)

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

<?php
$string = "apple123";

$isThereNumber = false;
for ($i = 0; $i < strlen($string); $i++) {
    if ( ctype_digit($string[$i]) ) {
        $isThereNumber = true;
        break;
    }
}

if ( $isThereNumber ) {
    echo "\"{$string}\" has number(s).";
} else {
    echo "\"{$string}\" does not have number(s).";
}

?>

Output

How do you check if a string contains a number in php?

Conclusion

In 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() Function

You can use the PHP strpos() function to check whether a string contains a specific word or not.

The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false. Also note that string positions start at 0, and not 1.

Let's check out an example to understand how this function basically works:

<?php
$word = "fox";
$mystring = "The quick brown fox jumps over the lazy dog";
 
// Test if string contains the word 
if(strpos($mystring, $word) !== false){
    echo "Word Found!";
} else{
    echo "Word Not Found!";
}
?>


Here are some more FAQ related to this topic:

  • How to count how many times a substring occurs in a string in PHP
  • How to replace a word inside a string in PHP
  • How to split a string into an array in PHP

In this guide, we will be showing you how you can check if a string is a number in PHP using is_numeric().

How do you check if a string contains a number in php?

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 PHP

Let 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 true. Otherwise, this function will return false.

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 Number

For 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 true.

We start this example by creating a variable called “$number” and assigning it the number 123.

Next, we utilize the is_numeric() function to check if the “$number” variable is numeric. We print this result by passing it directly into PHP’s var_dump() function.

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 “true“.

Checking if a Numeric String is a Number

For 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 true.

The script is started with the creation of the “$number” variable and its assignment with the string "1.25".

We then use the is_numeric() function to check whether our “$number” variable is, in fact, a numeric string. This is printed by wrapping the function call in “var_dump()“.

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-Number

Now 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 “$notanumber” and assign it the string "123Hello". Even though this string has a number, it is not a numeric string.

Next, we use the “is_numeric()” function to check and return whether the “$notanumber” string is numeric.

Since our string contains elements that PHP can’t evaluate into a number, this function will return false.

is_numeric() Evaluates All Number Types

PHP’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 “0xcf66” or a binary number like “0b10101” it will still be evaluated as “numeric”.

To showcase this, let us write a quick PHP example to test how the is_numeric() function handles the number 0xCF66.

Start this PHP script by creating a variable called “$hex” and assign it the value 0xCF66.

Like our previous examples, we use the “is_numeric()” function to check whether its numeric, and use “var_dump()” to print the resulting value.

Below you can see how the is_numeric() function evaluated our hexadecimal number as numeric.

Conclusion

In 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.