Summary of several methods for detecting data types in PHP

怪我咯
Release: 2023-03-13 06:52:02
Original
2343 people have browsed it

The following editor will bring you several methods (summary) of PHP detection data type. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

In JavaScript, use typeof to detect basic data types, and use instanceof to detect reference data types. In PHP, there are also methods to detect data types, as follows:

1. Output the data type of variable (gettype)

<?php 

   $arry = array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;); 

   echo gettype($arry);//array 

 ?>
Copy after login

2. Data type of output variable, included quantity and specific content (var_dump)

View source code printing code help

 <?php 

   $str = &#39;hello world&#39;; 

   var_dump($str);//string(11) "hello world"  

 ?>
Copy after login

3. Check whether a variable is of the specified data type (is_array, is_string, is_int, is_double, etc.). If it is true, it returns 1, if it is false, it returns empty.

View source code print code help

 <?php 

   $num = 123; 

   if(is_array($num)){ 

     echo &#39;这是一个数组&#39;; 

   }else if(is_string($num)){ 

     echo &#39;这是一个字符串&#39;; 

   }else if(is_int($num)){ 

     echo &#39;这是一个整数&#39;; 

   }else if(is_double($num)){ 

     echo &#39;这是一个浮点数&#39;; 

   } 

 ?>
Copy after login

The above is the detailed content of Summary of several methods for detecting data types in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!