Home >Backend Development >PHP Tutorial >Summary and techniques of several methods for detecting data types in PHP

Summary and techniques of several methods for detecting data types in PHP

黄舟
黄舟Original
2017-03-06 09:28:411234browse

The following editor will bring you several methods of detecting data types in PHP (summary). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. 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. Data type of output variable (gettype)

<?php 

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

   echo gettype($arry);//array 

 ?>

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"  

 ?>

3. Detect whether a variable is a specified data type (is_array, is_string, is_int, is_double, etc. ), returns 1 if true, returns null if false.

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

   } 

 ?>

Maybe your friends are looking for the answer to this question, Share it with him quickly!

The above is a summary of several methods and techniques for detecting data types in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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