PHP data type viewing and judging data type
We can proceed to the next step only after we know the type of data. Later, you can learn more knowledge - custom functions (functions).
Let’s do a scene simulation: (Note: You don’t need to know how to write this function now, I will teach you in the future)
Assume that we can write an intelligent function (function) that allows someone to One function is to open a spreadsheet containing student grades. This function is very advanced.
1. When there is only one person, the score of one person is returned. The score is an integer or floating point variable.
2. When there are two or more people, return an array type variable, and the array contains the results of multiple people
3. If there is no one, return a Boolean Value variable, the variable value is false
Through the above scenario simulation, we know that a function (function) may return different data types. So we can do different things based on different types.
Determining the data type is very important and we need to strictly control it.
[Dialogue-level knowledge points] Functions to display types, functions to obtain types, and functions to determine types
View data types
1.gettype (pass in a variable) to get the type of the variable
2.var_dump (pass in a variable) output the variable type and value
Determine the data type
We use the is_* series of functions. The is_types series of functions are used to determine whether something is of a certain type. Returns true if it is of this type, false if it is not.
is_int Whether it is an integer type
is_bool Whether it is a Boolean
is_float Whether it is a floating point
is_string Whether it is a string
is_array Whether it is an array
is_object Whether it is an object
is_null Whether it is empty
is_resource Whether it is a resource
is_scalar Whether it is a scalar
is_numeric Whether it is a numerical type
is_callable Whether it is a function
"; //字符串类型的数值,看看执行的是真还是假 $str = '18.8'; if(is_numeric($str)){ echo '执行真区间'; }else{ echo '执行假区间'; } echo "
"; //把sca的值换成整型、浮点、布尔和字符串试试 $sca = true; //如果为标量,执行真区间 if(is_scalar($sca)){ echo '执行真区间'; }else{ echo '执行假区间'; } echo "
"; //换成echo,is_int试试,为什么echo执行假区间呢? if(is_callable('var_dump')){ echo '执行真区间'; }else{ echo '执行假区间'; } ?>
Note: is_types is easy to remember . Just follow the type before and after is_.
Variables: int, float, bool, string
Mixed types: array, object
Special types: null, resource, callback
Why did echo execute a false interval in the final experiment?
Answer: Because echo is not a function, it is basic syntax. You don’t need to understand the difference between functions and basic syntax right now. Understanding and not understanding have no impact in actual development. Just know that you have this knowledge.