PHP isset and empty Functions

DistinguishedFern avatar
DistinguishedFern
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is the purpose of the isset() function in PHP?

To check if a variable is set and is not NULL

What will be the result of isset($a) if $a is not declared?

FALSE

What is the purpose of the empty() function in PHP?

To check if a variable is empty

What will be the result of empty($a) if $a is an empty string?

TRUE

What is the difference between isset() and is_null()?

isset() checks if a variable is set and is not NULL, while is_null() checks if a variable is NULL

What will be the result of isset($a) if $a is NULL?

FALSE

What is the purpose of the null coalescing operator (??) in PHP?

To return the first operand if it exists and is not NULL, and the second operand otherwise

What will be the result of $a ?? 'default' if $a is an empty string?

An empty string

How can you check if a variable does not exist in PHP?

Using the !isset() function

What is the result of isset($a) if $a is declared but not assigned a value?

FALSE

Study Notes

Isset Usage

  • isset() is a PHP function used to check if a variable is set and is not NULL.
  • It returns TRUE if the variable exists and has a value other than NULL.
  • It returns FALSE if the variable does not exist or is NULL.

Example:

$a = 'hello';
$b = NULL;

isset($a); // returns TRUE
isset($b); // returns FALSE

Is Empty Examples

  • empty() is a PHP function used to check if a variable is empty.
  • It returns TRUE if the variable is empty (i.e., it does not exist or its value equals FALSE).
  • It returns FALSE if the variable is not empty.

Examples:

$a = '';
$b = 'hello';
$c = 0;
$d = NULL;

empty($a); // returns TRUE
empty($b); // returns FALSE
empty($c); // returns TRUE
empty($d); // returns TRUE

Isset Vs Is Null

  • isset() and is_null() are two distinct PHP functions:
    • isset() checks if a variable is set and is not NULL.
    • is_null() checks if a variable is NULL.
  • isset() is used to check if a variable exists, while is_null() is used to check if a variable is NULL.

Example:

$a = NULL;

isset($a); // returns FALSE
is_null($a); // returns TRUE

Variable Existence Checks

  • In PHP, a variable is considered to exist if it has been declared and assigned a value.
  • To check if a variable exists, use the isset() function.
  • To check if a variable does not exist, use the !isset() function.

Example:

$a = 'hello';

isset($a); // returns TRUE
!isset($b); // returns TRUE, since $b is not declared

Null Coalescing Operator

  • The null coalescing operator (??) is a PHP operator used to return the first operand if it exists and is not NULL.
  • If the first operand is NULL, it returns the second operand.
  • It is used to simplify the process of checking for NULL values.

Example:

$a = 'hello';
$b = NULL;

$c = $a ?? 'default'; // returns 'hello'
$d = $b ?? 'default'; // returns 'default'

Isset Usage

  • isset() checks if a variable is set and is not NULL.
  • It returns TRUE if the variable exists and has a value other than NULL.
  • It returns FALSE if the variable does not exist or is NULL.

Is Empty Examples

  • empty() checks if a variable is empty, meaning it does not exist or its value equals FALSE.
  • It returns TRUE if the variable is empty.
  • It returns FALSE if the variable is not empty.
  • Examples of empty values include '', 0, and NULL.

Isset Vs Is Null

  • isset() and is_null() are distinct PHP functions.
  • isset() checks if a variable is set and is not NULL.
  • is_null() checks if a variable is NULL.
  • isset() is used to check if a variable exists, while is_null() is used to check if a variable is NULL.

Variable Existence Checks

  • A variable is considered to exist if it has been declared and assigned a value.
  • isset() is used to check if a variable exists.
  • !isset() is used to check if a variable does not exist.

Null Coalescing Operator

  • The null coalescing operator (??) returns the first operand if it exists and is not NULL.
  • If the first operand is NULL, it returns the second operand.
  • It is used to simplify the process of checking for NULL values.
  • It can be used to provide a default value when a variable is NULL.

Learn about the isset and empty functions in PHP. Understand how to use them to check if a variable is set, not NULL, or empty.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

PHP Functions Quiz
6 questions

PHP Functions Quiz

TrustingPeridot avatar
TrustingPeridot
PHP Functions and File Uploading Quiz
32 questions
PHP: Functions, Classes, Variables
5 questions
Web Development Lecture 9: PHP Loops and Functions
5 questions
Use Quizgecko on...
Browser
Browser