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
<?php //聲明一個(gè)變量88.8,你可以自己多做幾次實(shí)驗(yàn)換成其他類型看看$type輸出是多少 $float = 88.8; $type = gettype($float); echo $type; ?>
<?php //多換幾個(gè)類型試試 $str = '你喜歡尊上還是喜歡殺仟陌?'; var_dump($str); ?>
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
<?php //is_* 系列函數(shù)有個(gè)特點(diǎn),就是如果是這個(gè)類型就返回的是真。不是這個(gè)數(shù)據(jù)類型就返回的是假 //聲明類型為假 $fo = false; if(is_bool($fo)){ echo '執(zhí)行真區(qū)間'; }else{ echo '執(zhí)行假區(qū)間'; } //檢查未聲明的變量$p是否為空,為空就執(zhí)行真區(qū)間 if(is_null($p)){ echo '執(zhí)行真區(qū)間'; }else{ echo '執(zhí)行假區(qū)間'; } echo "<br/>"; //字符串類型的數(shù)值,看看執(zhí)行的是真還是假 $str = '18.8'; if(is_numeric($str)){ echo '執(zhí)行真區(qū)間'; }else{ echo '執(zhí)行假區(qū)間'; } echo "<br/>"; //把sca的值換成整型、浮點(diǎn)、布爾和字符串試試 $sca = true; //如果為標(biāo)量,執(zhí)行真區(qū)間 if(is_scalar($sca)){ echo '執(zhí)行真區(qū)間'; }else{ echo '執(zhí)行假區(qū)間'; } echo "<br/>"; //換成echo,is_int試試,為什么echo執(zhí)行假區(qū)間呢? if(is_callable('var_dump')){ echo '執(zhí)行真區(qū)間'; }else{ echo '執(zhí)行假區(qū)間'; } ?>
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.