Home Backend Development PHP Tutorial PHP equality comparison and empty, isset, isnull

PHP equality comparison and empty, isset, isnull

Jul 29, 2016 am 08:59 AM
dump false null true var

Variable is empty

The following are considered empty:

  • "" (empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a floating point number)
  • "0 ” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared but without a value)

The null value in the judgment statement returns false , the following code will not produce any output, because the judgments in the if statement are all false:

<code><span>$emptyList</span> = [<span>""</span>,  <span>0</span>, <span>0.0</span>, <span>"0"</span>, <span>NULL</span>, <span>FALSE</span>, <span>array</span>(), <span>$var</span>, ];
<span>foreach</span>(<span>$emptyList</span><span>as</span><span>$val</span>) {
    <span>if</span> (<span>$val</span>) {
        var_dump(<span>$val</span>);
    }
}</code>
Copy after login

Comparison operators: == and ===

  • == only involve the comparison of values ​​
<code>var_dump(<span>0</span> == <span>'0.0'</span>); <span>//</span><span>true</span>
var_dump(<span>0</span> == <span>''</span>); <span>//</span><span>true</span>
var_dump(<span>0</span> == <span>false</span>); <span>//</span><span>true</span></code>
Copy after login
  • === It also involves the comparison of values ​​and types, which is more strict.
<code>var_dump(<span>0</span> === <span>'0.0'</span>); <span>//</span><span>false</span>
var_dump(<span>0</span> === <span>''</span>); <span>//</span><span>false</span>
var_dump(<span>0</span> === <span>false</span>); <span>//</span><span>false</span></code>
Copy after login
  • null is compared with the null value through ==. Except for "0", all return true:
<code><span><span><?php</span><span>$emptyList</span> = [<span>""</span>,  <span>0</span>, <span>0.0</span>, <span>"0"</span>, <span>NULL</span>, <span>FALSE</span>, <span>array</span>(), <span>$var</span>, ];
<span>foreach</span>(<span>$emptyList</span><span>as</span><span>$val</span>) {
    var_dump(<span>$val</span> == <span>null</span>);
}</span></code>
Copy after login

empty , isset, isnull

  • empty: Check whether a variable is empty
<code><?php
<span>$emptyList</span> = [<span>""</span>,  <span>0</span>, <span>0.0</span>, <span>"0"</span>, NULL, FALSE, array(), <span>$var</span>, ];
foreach (<span>$emptyList</span> as <span>$e</span>) {
    var_dump(empty(<span>$e</span>));
}
/*
bool(<span>true</span>)
bool(<span>true</span>)
bool(<span>true</span>)
bool(<span>true</span>)
bool(<span>true</span>)
bool(<span>true</span>)
bool(<span>true</span>)
bool(<span>true</span>)
*/</code>
Copy after login
  • isset: Return TRUE if the variable var exists and the value is not NULL, otherwise return FALSE.
<code><span>$var</span> = <span>1</span>;
var_dump(<span>isset</span>(<span>$var</span>)); <span>// true</span><span>unset</span>(<span>$var</span>);
var_dump(<span>isset</span>(<span>$var</span>)); <span>// false</span><span>$var</span> = <span>null</span>;
var_dump(<span>isset</span>(<span>$var</span>)); <span>// false</span></code>
Copy after login
  • isnull: Determine whether the variable is null, equivalent to === null.

Note that the

==

comparison between null values ​​may not necessarily return true:

<code><span>// 不具传递性</span><span>var_dump(<span>0</span> == <span>'0'</span>)</span>; <span>// true</span><span>var_dump(<span>''</span> == <span>0</span> )</span>; <span>// true</span><span>var_dump(<span>'0'</span> == <span>''</span>)</span>; <span>// false</span><span>var_dump(<span>0</span> == [])</span>; <span>// false</span></code>
Copy after login

Best practice

  • Use empty to judge null values;
  • isset cannot judge variables that exist but have a null value; The difference between
  • php == and === is that the latter performs type equality judgment at the same time
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP equality comparison and empty, isset, isnull, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is a dump file? What is a dump file? Jan 12, 2024 pm 04:58 PM

A dump file usually refers to a binary file, also known as a dump file or core dump file. This kind of file is generated by the computer system when it encounters a serious error or abnormal situation. It is used to store the status, stack, registers, memory images, logs and other information of the system or application.

What is the difference between null and NULL in c language What is the difference between null and NULL in c language Sep 22, 2023 am 11:48 AM

The difference between null and NULL in C language is: null is a macro definition in C language, usually used to represent a null pointer, which can be used to initialize pointer variables, or to determine whether the pointer is null in a conditional statement; NULL is a macro definition in C language A predefined constant in , usually used to represent a null value, used to represent a null pointer, null pointer array or null structure pointer.

What do undefined and null mean? What do undefined and null mean? Nov 20, 2023 pm 02:39 PM

In JavaScript, both undefined and null represent the concept of "nothing": 1. undefined represents an uninitialized variable or a non-existent property. When a variable is declared but no value is assigned to it, the value of the variable is undefined , when accessing properties that do not exist in the object, the returned value is also undefined; 2. null represents an empty object reference. In some cases, the object reference can be set to null to release the memory it occupies.

Let's talk about the differences between var, let and const (code example) Let's talk about the differences between var, let and const (code example) Jan 06, 2023 pm 04:25 PM

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

When to use null and undefined When to use null and undefined Nov 13, 2023 pm 02:11 PM

Both null and undefined indicate a lack of value or an undefined state. Depending on the usage scenario, there are some guiding principles for choosing to use null or undefined: 1. When you need to clearly indicate that a variable is empty or invalid, you can use null; 2. When a variable has been declared but not yet assigned a value, it will be set to undefined by default; 3. When you need to check whether a variable is empty or undefined, use the strict equality operator "===" to determine whether the variable is null or undefined. .

What is the difference between null and undefined What is the difference between null and undefined Nov 08, 2023 pm 04:43 PM

The difference between null and undefined is: 1. Semantic meaning; 2. Usage scenarios; 3. Comparison with other values; 4. Relationship with global variables; 5. Relationship with function parameters; 6. Nullability check; 7. Performance considerations; 8. Performance in JSON serialization; 9. Relationship with types. Detailed introduction: 1. Semantic meaning, null usually means knowing that this variable will not have any valid object value, while undefined usually means that the variable has not been assigned a value, or the object does not have this attribute; 2. Usage scenarios, etc.

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

What are the uses of null in java What are the uses of null in java Mar 01, 2024 am 10:10 AM

Usage: 1. Initialize the reference type variable to null, indicating that the variable does not currently point to any object; 2. Set the reference type variable to null, which can release the memory space of the object referenced by the variable and help the garbage collector to recover This object; 3. Use null to check whether a reference is empty. You can avoid the occurrence of NullPointerException by judging whether the reference is null. 4. Use null in conditional judgment to judge whether a reference is empty.

See all articles