Home > Backend Development > PHP Tutorial > PHP determines 0 and empty PHP instances through various functions

PHP determines 0 and empty PHP instances through various functions

jacklove
Release: 2023-04-01 18:32:02
Original
2122 people have browsed it

This article introduces to you the method of judging 0 and empty in PHP and various functions. In the article, I also introduce to you the solution that 0 is not equal to null in PHP syntax. Interested friends can follow the editor of PHP Chinese website. Learn

The function’s judgment of 0

   $cast_id = 0;
    var_dump(strlen($cast_id));  //1
    var_dump(empty($cast_id)); // true
    var_dump(isset($cast_id)); //true
    var_dump(is_null($cast_id));//false
Copy after login

The judgment of empty

    $cast_id = "";
    var_dump(strlen($cast_id));  //0
    var_dump(empty($cast_id)); // true
    var_dump(isset($cast_id)); //true
    var_dump(is_null($cast_id));//false
Copy after login

Supplement: Let me introduce to you the solution to the problem that 0 is not equal to null in PHP syntax

I encountered such a problem today: In the php statement, I want to determine whether a value is greater than or equal to 0. I use ($value !=null && $value >=0), The returned result is empty, which is really strange.

Experiment summary:

php statement is as follows:

$index=0;
echo "A: ".$index."<br>"; //0
echo "B: ".($index !=null && $index >=0)."<br>";//
echo "C: ".(isset($index) && $index >=0)."<br>";//1
echo "D: ".(0 !=null)."<br>";//
Copy after login

Result:

A : 0
B:
C: 1
D:

To determine whether a value [the array may be empty, etc.] is greater than or equal to 0, another method can be used : is_numeric($index) === true

$index=array_search($url, $contentOtherStr, true);
 //值大于等于0, 即存在
if(is_numeric($index) === true)
 {
echo "$url existed. "."<br>";
 }else{
echo "$url Add. "."<br>";
array_push($contentOtherStr, $url);
 }
Copy after login

This is very strange and finally solved. Mark.

Summary: PHP's statements are a little weird. Students who have transferred from other programming languages ​​must be more careful and pay attention to inertial thinking and grammatical differences to avoid falling into pitfalls.

Other information:

The reason is that variables in PHP are stored in C language structures. Empty strings, NULL, and false are all based on values. 0 is stored, and this structure has a zend_uchar type; such a member variable is used to save the type of the variable, and the type of the empty string is string, the type of NULL is NULL, and false is boolean.

You can use echo gettype(''); and echo gettype(NULL); to print this! The === operator not only compares values, but also compares types, so the third one is false!

In addition, in php

= One equal sign is assignment
== Two equal signs are used to judge equality and only compare values, not types
=== Three equal signs are used to judge that both values ​​and types are equal
!= Not equal to the sign, only compare values, regardless of type
!== Not equal to the sign, compare values ​​and types

So Empty string (''), false, NULL and 0 are equal in value but different in type!

Note:

NULL is a special type.

It is NULL in two cases

1. $var = NULL;
2. $var;
3."", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty. If var is empty, Returns TRUE.

Articles you may be interested in:

php tips for identifying upside-down pictures taken by flipping the iPhone

PHP implements login verification code verification function php example

How to use Composer to install ThinkPHP5 in a windows environment

The above is the detailed content of PHP determines 0 and empty PHP instances through various functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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