search
HomeBackend DevelopmentPHP TutorialPHP determines 0 and empty PHP instances through various functions

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

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

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>";//

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);
 }

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!

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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 Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools