PHP is a loosely typed programming language, and it is an indispensable step in a function to examine the "type" of the passed parameter value and whether the value is null or NULL.
Starting with PHP5, PHP allows type constraints on the parameters of a function, that is, the type of the parameter can be constrained to an object, an interface, an array (starting with PHP 5.1), or callable (PHP5.4), but it is not possible to constrain the parameter type to a scalar type (such as int, string,bool). It is also not possible to constrain the traits type. If not constrained, by default, the type of the parameter is mixed, which means that all types can be accepted.
- function test (array $names)//correct procedure
- function test (string $name)//Wrong practice
For a function function of a particular parameter type, it is necessary to type check the parameters of the parameter in the function (although I have seen some articles from foreign scholars, they do not recommend this, which is equivalent to the function of binding functions).
In PHP, you can get the type of the variable by GetType and Get_resource_type two functions, which return the type of the parameter and the string of the resource type, and you can do a string comparison directly to determine whether the type is correct. Another way is to determine whether the type of the variable is a specified type, such as is_array,is_scalar,is_resource,is_string, by using the is_*** series function.
- Whether a variable/constant is defined or initialized
In PHP, a variable does not need to be initialized after it is defined, it is initialized to a default value with its type, but it is not recommended, and it is generally better to display initialization of the variable.
Whether a variable is explicitly initialized can be judged using the syntax structure-isset, which can determine whether a variable has been explicitly initialized. Isset returns true only if the variable "is not explicitly assigned or is assigned null", other cases, such as a value of NULL string, 0, and so on.
You can use unset to delete a variable that has already been defined.
Isset and unset can only be used to determine and delete the "variable" definition. If it is for "constant", then you must use defined to judge. If it is for "function", then it must be judged using the function_exist () function.
- To determine if a variable is null
There are many ways to determine whether a variable is null in PHP:
Is_null VS Isset
Both of these functions can be used to determine if a variable is null, and they have the same identity for an empty string, 0,false. namely is_null=! Isset ().
But Isset is a grammatical structure, and is_null is a function. In performance, the grammatical structure is relatively good. So it is recommended to use Isset instead of is_null in many places.
= = VS = = =
In some cases, it is recommended to use Isset to determine if a variable is null.
But semantically, whether a variable is "initialized" and "null" is a different concept, it is inappropriate to use isset in some scenarios, such as checking if the return value of a function is null.
You can now use "= =" and "= = =" To determine whether they are null.
for "= =" and "= = =", their direct difference is still very large. for "= =", it identifies an empty string and 0,false is null. for "= = =", only one variable is really null, which represents NULL.
In addition, "= = =" Relative to the "Isset", the performance is basically close, even better.
therefore, the best way to determine whether a variable is null is to use "= = =" Directly, so that there is no hesitation between is_null,isset. In fact, the above conclusions are also in the same vein as false judgments.
- Understanding of the empty () function
When judging whether a variable is null, the empty function is also discussed with Isset and the like. In fact, this function does not need to compare with them, because it represents the meaning is very simple, it means to determine whether a variable is stored in zero or empty, and here is not only null. So what does it mean to be 0 or empty? "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties will be considered empty, representing zero.
-
- If $e is undefined or has a value of nulll, $e =null, it is definitely empty, i.e. empty ($e) =true;
- If $e is of type int, $e = 0, relative to the number, 0 is 0 , i.e. empty ($e) =true;
- If $e is a string type, $e = "", relative to the string, "" is represented as null, i.e. empty ($e) =true;
- If $e is a string type, $e = "0", relative to the string, "0" is represented as 0 , which is empty ($e) =true;
- If the $e is of type bool, $e =false, the relative true,false is null, i.e. empty ($e) =true;
- If $e is an array type, $a =array (), relative to the array, no element of the data is empty, namely empty ($e) =true;
Empty () is the antonym of (Boolean) Var, except that it does not produce a warning when the variable has no value.
Type constraint: http://www.php.net/manual/zh/language.oop5.typehinting.php
Variable handling function: http://www.php.net/manual/zh/ref.var.php
PHP Type comparison table: http://www.php.net/manual/zh/types.comparisons.php
This article from http://www.cnblogs.com/niejunlei/p/5318826.html
PHP type judgment and null, NULL check