There are many methods to judge whether a string is null in php, such as null = null, empty, and isset, which can be used to determine whether a variable or string is null, next I will introduce it to you.
String; judge whether the string is null; Output judgment; you can modify it
| The Code is as follows: |
Copy code |
If (empty ($ C_char) return false; // whether it is set If ($ C_char = '') return false; // whether it is null |
Use = ""
Example
| The Code is as follows: |
Copy code |
<? Php $ Str = ''; If ($ str = '') {//'' = null ''= false ''! = False Echo 'str is a NULL string .'; } ?> |
Empty determines whether it is null
| The Code is as follows: |
Copy code |
<? Php $ Var = 0; // The result is true because $ var is null. If (empty ($ var )){ Echo '$ var is either 0 or not set at all '; } // The result is false because $ var has been set. If (! Isset ($ var )){ Echo '$ var is not set at all '; } ?> |
On the surface, it is easy to misunderstand that the empty () function is a function used to judge whether the string is null. In fact, it is not, so I suffered a lot of losses.
The empty () function is used to test whether the variable has been configured. If the variable already exists, non-null string, or non-zero, false is returned. Otherwise, false is returned.
True. Therefore, if the value of the string is 0, true is returned, that is, the internal statement of empty is executed. This is a trap.
For example, suppose $ value = 0; then empty ($ value) = false.
To judge whether the string is null, you can judge it as follows: if ($ value = "")...
* Format: bool empty (mixed var)
* Function: Check whether a variable is empty.
* Return value:
* Returns TRUE if the variable does not exist.
* If a variable exists and its values are "", 0, "0", NULL, FALSE, array (), var $ var, and objects without any attributes, the system returns
TURE
* If a variable exists and its values are not "", 0, "0", NULL, FALSE, array (), var $ var, or an object without any attribute
FALSE
Isset () and empty () judgment methods:
| The Code is as follows: |
Copy code |
Function demo (){ $ Var = _ post ['a']; // The accepted parameter. Echo "isset test: <Br> "; If (isset ($ var) n { Echo 'variable $ var exists! <Br> '; } Else { Echo 'variable $ var does not exist! <Br> '; } Echo "empty test: <Br> "; If (empty ($ var )){ Echo 'variable $ var value is empty <Br> '; } Else {
Echo 'variable $ var value is not empty <Br> '; } Echo "Direct Test of variables: <Br> "; If ($ var ){
Echo 'variable $ var exists! <Br> '; } Else { Echo 'variable $ var does not exist! <Br> '; } |