search
HomeBackend DevelopmentPHP TutorialWhat will php's var_dump(1...9) output?

What will php's var_dump(1...9) output?

Apr 13, 2020 am 09:18 AM
phpvar_dump()

What will var_dump(1...9) output? The following article will analyze it for you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What will php's var_dump(1...9) output?

A question, var_dump(1...9)What is the output?

Verify it by hand:

php -r “var_dump(1...9)”;
string(4) ”10.9“

Output 10.9. At first glance, the output of this var_dump seems strange, right? why?

Here we will teach you, if you see a piece of PHP code and feel that the output is strange, the first reaction is to see what the opcodes generated by this code are. Although this problem is actually a problem in the lexical analysis stage, but still use Let's analyze it with phpdbg (usually in order to prevent the influence of opcache, -n will be passed):

phpdbg -n -p /tmp/1.php
function name: (null)
L1-35 {main}() /tmp/1.php - 0x7f56d1a63460 + 4 ops
L2 #0 INIT_FCALL<1> 96 "var_dump"
L2 #1 SEND_VAL "10.9" 1
L2 #2 DO_ICALL
L35 #3 RETURN<-1> 1

So it seems that long before the opcode is generated, 1...9 becomes the constant 10.9, considering This is a literal value. Let’s take a look at zend_language_scanner.l and find this line:

DNUM ({LNUM}?"."{LNUM})|({LNUM}"."{LNUM}?)

This is the format of floating point numbers defined by lexical analysis. It suddenly dawned on us at this point:

1 ...9 will be accepted in sequence as: 1. (floating point number 1), then . (string concatenation symbol) and then .9 (floating point number 0.9)

so it will be generated directly during the compilation phase "1" . "0.9" -> String literal "10.9"

Okay, here, this little "puzzle" is explained clearly.

Of course, this is not only defined in PHP, almost all languages ​​will define this abbreviated floating-point form. Sometimes in C language, in order to input a floating-point integer, we can Use for example 1. to tell the compiler that this is a floating point number.

However, firstly, it happens to be in PHP. The number has another meaning, which is string concatenation. Secondly... in PHP5.6 Then there is a new operator called the Splat operator, which can be used to define variable parameter functions or solve arrays, for example,

<?php
 
function  foo($a, $b, $c) {
        var_dump($a + $b + $c);
}
 
 
$parameters = array (1, 2, 3);
 
foo(...$parameters);
?>

So, at first glance, this leads to this confusing result

Recommended learning: PHP video tutorial

The above is the detailed content of What will php's var_dump(1...9) output?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:laruence. If there is any infringement, please contact admin@php.cn delete
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

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use