How does PHP determine whether a variable is empty and has a value? (Picture + video tutorial)

藏色散人
Release: 2019-11-09 18:01:31
Original
14937 people have browsed it

During the development of a PHP project or when a novice is learning PHP, you may encounter prompts such as a certain variable does not exist or a certain variable is empty. At this point we need to know how PHP determines whether a variable is empty or whether it exists. This question is also one of the common PHP interview questions.

This article will give you a detailed introduction to the specific methods of PHP to determine whether a variable is empty and PHP to determine whether a variable exists.

Below we will introduce it to you in detail through a simple code example.

1. The isset function determines whether the variable exists.

<?php
$a = &#39;&#39;;
var_dump(isset($a));
Copy after login

Here we use the isset function to determine whether the $a variable exists. The judgment results are as follows Picture:

How does PHP determine whether a variable is empty and has a value? (Picture + video tutorial)

#If true is shown in the picture, it means that the variable $a exists.

<?php
$a = &#39;&#39;;
var_dump(isset($x));
Copy after login

Determine whether the $x variable exists. The judgment result is as follows:

How does PHP determine whether a variable is empty and has a value? (Picture + video tutorial)

If false is shown in the figure, it means that the variable $x does not exist. .

Summary 1: The isset function in PHP is used to detect whether the variable exists, is set and is not NULL. Returns TRUE if a variable exists and its value is not NULL, FALSE otherwise.

2. The empty function determines whether the variable is empty.

<?php
$a = &#39;&#39;;
var_dump(empty($a));
Copy after login

Here we use the empty function to determine whether the value of the variable $a is empty. The result is as shown below:

How does PHP determine whether a variable is empty and has a value? (Picture + video tutorial)

We will enter a space into the variable $a:

<?php
$a = &#39; &#39;;//此处有空格
var_dump(empty($a));
Copy after login

The result printed at this time is as follows:

How does PHP determine whether a variable is empty and has a value? (Picture + video tutorial)

Summary 2: The empty function in PHP is used to check whether a variable is empty. Returns FALSE when a variable exists and is a non-null, non-zero value Otherwise, return TRUE.

The above is an introduction to the specific methods of PHP to determine whether a variable is empty and determine whether a variable exists. Hope it helps those in need!

If you want to know more about PHP, you can follow the PHP video tutorial on the PHP Chinese website. Everyone is welcome to refer to and learn!

The above is the detailed content of How does PHP determine whether a variable is empty and has a value? (Picture + video tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!