


is_numeric() function in PHP: How to check if a variable is numeric
In PHP, it is often necessary to judge and verify the data type of variables. Among them, determining whether a variable is of numeric type is a very common operation. PHP provides a built-in function is_numeric() to check whether a variable is of numeric type.
The usage format of is_numeric() function is as follows:
bool is_numeric (mixed $var)
Among them, $var is the variable that needs to be checked, it can be any PHP data Type, including string, integer, floating point, etc. When $var is a number or numeric string, the is_numeric() function returns true, otherwise it returns false.
Below we use specific code examples to illustrate the use of the is_numeric() function.
- Check integer variables
Sample code:
<?php $num1 = 123; $num2 = "456"; $num3 = "789hello"; if (is_numeric($num1)) echo '$num1是数字'; if (is_numeric($num2)) echo '$num2是数字'; if (is_numeric($num3)) echo '$num3是数字'; ?>
Output result:
$num1是数字 $num2是数字
Analysis:
We have defined three variables $num1, $num2 and $num3, where $num1 is an integer and $num2 and $num3 are numeric strings. By judging these three variables through the is_numeric() function, you can see that $num1 and $num2 are both recognized as numbers, and $num3 is judged as a non-numeric variable because it contains the non-numeric character "hello".
- Check floating point variables
Sample code:
<?php $num1 = 12.34; $num2 = "56.78"; $num3 = "90.12hello"; if (is_numeric($num1)) echo '$num1是数字'; if (is_numeric($num2)) echo '$num2是数字'; if (is_numeric($num3)) echo '$num3是数字'; ?>
Output result:
$num1是数字 $num2是数字
Analysis:
This time we defined three floating-point numeric variables $num1, $num2 and $num3, where $num1 and $num2 are floating-point numbers, and $num3 contains non-numeric characters. By judging these three variables through the is_numeric() function, you can see that $num1 and $num2 are both recognized as numbers, while $num3 is judged as a non-numeric variable.
- Check other data type variables
Sample code:
<?php $str1 = "Hello World"; $str2 = "1234"; $bool1 = true; $arr1 = array('a', 'b', 'c'); if (is_numeric($str1)) echo '$str1是数字'; if (is_numeric($str2)) echo '$str2是数字'; if (is_numeric($bool1)) echo '$bool1是数字'; if (is_numeric($arr1)) echo '$arr1是数字'; ?>
Output result:
$str2是数字
Parsing:
This time we defined four different data type variables $str1, $str2, $bool1 and $arr1. $str1 is a string type, $str2 is a numeric string type, $bool1 is a Boolean type, and $arr1 is an array type. Through the is_numeric() function to judge these four variables, you can see that only $str2 is judged to be a numeric type, and the other three variables are not numeric types.
Through these specific code examples, we can see how to use the is_numeric() function and the judgment rules. In actual PHP development, in many cases it is necessary to judge and verify the data type of variables. The is_numeric() function is a very convenient built-in function that can help us quickly judge the numeric type of variables.
The above is the detailed content of is_numeric() function in PHP: How to check if a variable is numeric. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass
![Internal error: Unable to create temporary directory [Resolved]](https://img.php.cn/upload/article/000/000/164/168171504798267.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

PHP function introduction—strpos(): Check whether a variable is a string In PHP, is_string() is a very useful function, which is used to check whether a variable is a string. When we need to determine whether a variable is a string, the is_string() function can help us achieve this goal easily. Below we will learn about how to use the is_string() function and provide some related code examples. The syntax of the is_string() function is very simple. it only needs to

In the PHP programming language, the is_numeric() function is a very commonly used function, used to determine whether a variable or value is a number. In actual programming, it is often necessary to verify the value entered by the user to determine whether it is a numeric type. In this case, the is_numeric() function can be used to determine. 1. Introduction to is_numeric() function The is_numeric() function is a function used to detect whether a variable or value is a number. Returns tru if the variable or value is a number
