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

PHP determines 0 and empty through various functions

Lisa Kudrow
Lisa Kudrow Original
2018-06-01 11:32:01 3105browse

This article mainly introduces how PHP determines 0 and empty through various functions. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

Function pair 0 Judgment

$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

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

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 like this: In the php statement, I want to determine whether a value is greater than or equal to 0. I use ($value !=null && $value >=0), and the returned result is empty, which is really strange.

Experiment summary:

php statement is as follows:

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

Result:

A: 0 B: C: 1 D:

To judge a If the 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. "."
"; }else{ echo "$url Add. "."
"; array_push($contentOtherStr, $url); }

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 useecho gettype('');andecho 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

SoEmpty 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.

Related recommendations:

PHP custom function to implement assign() array assignment to template and extract() variable assignment to template

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

Statement:
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