top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

PHP Empty() vs Isset() PHP vs is_null()

+2 votes
1,619 views

PHP has different functions like Empty() Isset() and is_null(). It is used to test the value of variables. Read more to understand different scenarios it is useful.

What is empty() in PHP?

The function empty() will use to determine whether the variable is empty or not. Normally in two scenarios it will be considered as an empty.
1) If the variable does not exist. The particular variable which we are using in the empty function which does not exist.
Empty in PHP - case 1

Output:
It will return true. Because above code does not have variable called $a.

2) If the variable is set as a false. That time also it will be considered as an empty.
Empty in PHP - case 2

Output:
It will return true because the variable $a is set as false.

Also, no warning will be generated even if the variable does not exist and this empty() function will only support variables.

What is isset() in PHP?

The isset() function is used to determine whether the variable is set or not. If the variable is set it will return true otherwise it will return false.

Isset in PHP - case 1
Output:
This is a fruit name.

After setting the variable if the user will unset() the variable means then isset() function will be false.
Isset in PHP - case 2

Output:
This is a fruit name
After unset
Variable is not set

What is is_null() in PHP?

This is_null() function is used to check whether the variable is null or not. If the variable is null it will return true otherwise false.

is_null in PHP

Companionship among empty(), isset() and is_null() in PHP

In PHP these three functions (empty(), isset(), is_null()) are mainly used to check the value of the variable. Many a times isset() and empty() will be used but both are opposite behavior. The is_null() function also one of the important function but it will only used for checking if the variable is null or not.

posted Oct 20, 2014 by Salil Agrawal

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Substr in PHP

Substr means Sub String in english and that's what substr() function used i.e. it returns a part of string. Using substr() function in php we can split a string part by part. This function is present in almost in all language and have the similar syntax and meaning.

Syntax:

substr(string, start, length)

String – This is the input string which we need to split and a mandatory field.
Start – This is the starting Position from which we need to return the substring it can be positive, negative and zero. This is a mandatory field.
Position Values - Starting the String from the beginning.
Negative Values – Starting the String from end.
0 (Zero) – Zero means it will start from first character.
length - It is an optional field and can be positive, negative or zero. If length is not present then substring starting from start until the end of the string will be returned.

Positive Length: the string returned will contain beginning from start till start+length.
Negative Length: the string returned will contain beginning from start till end+length.
Zero Length: Empty string is returned.

SubstrExample

strstr() in PHP

strstr() in php is used to search a string within another string. This is a case sensitive function and is used to search first occurrence string.

Syntax:

Strstr(string, search, before_string);

String: This is mandatory field. This is the input string in which we need to search the search string.
Search: This is mandatory field and a string which needs to be searched.
Before_String: This is optional and default value is false. If we set as true,it will return the string before the first occurrence of string.

Example for strstr without using third parameter
strstr examples

Example for strstr with using third parameter
strstr example

READ MORE
...