PHP isset() vs empty() vs is_null()
PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.
Bạn đang xem: Bài 26: hàm isset() và empty() trong php
isset() & empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.
From PHP manual – isset():
isset — Determine if a variable is set and is not NULL
In other words, it returns true only when the variable is not null.
empty()From PHP Manual – empty():
empty — Determine whether a variable is empty
In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, và an unset variable.
is_null()From PHP Manual – is_null():
is_null — Finds whether a variable is NULL
In other words, it returns true only when the variable is null. Is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to lớn declared variables.
The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
|“” (an empty string)||bool(true)||bool(true)|
|” ” (space)||bool(true)|
|array() (an empty array)||bool(true)||bool(true)|
|“0″ (0 as a string)||bool(true)||bool(true)|
|0 (0 as an integer)||bool(true)||bool(true)|
|0.0 (0 as a float)||bool(true)||bool(true)|
|var $var; (a variable declared, but without a value)||bool(true)||bool(true)|
|NULL byte (“ 0″)||bool(true)|
What is Ajax
Absolute, Relative, Fixed Positioning: How bởi vì They Differ?
Short answer: Yes, there is one more, "static", which is the default. Yes, they majorly differ. Each of them is incredibily useful & which you should ...Chi tiết
CSS Font-Size: em vs. Px vs. Pt vs. Percent
One of the most confusing aspects of CSS styling is the application of the font-size attribute for text scaling. In CSS, you’re given four different units by which you can ...Chi tiết
How can I make Java print quotes, like “Hello”?
System.out.print(""Hello""); The double quote character has to lớn be escaped with a backslash in a Java string literal. Other characters that need special treatment ...Chi tiết
List diffirences between Java & C++
List diffirences between Java & C++Java ...Chi tiết
How to Create CSS3 Speech Bubbles Without Images
Credit: https://www.sitepoint.com/pure-css3-speech-bubbles/I remember my creating my first ...