深入解析PHP中逗号与点号的区别
复制代码 代码如下:
echo 'abc'.'def'; //用点号连接字符串
echo 'abc','def'; //用逗号连接字符串
那么下面我们就举一些例子.来认清楚他们之前的区别.
复制代码 代码如下:
echo '1+5=' . 1+5;
看看上面的.输出的结果是6..而不是1+5=6.有些神奇吧?
更神奇的是你看下面的例子.
复制代码 代码如下:
echo "1+5=" . 5+1; //输出2
结果十分奇怪.我们看到.我们把5和1换下位置.结果就变成2了.
为什么会这样.难道在PHP中加法是没有交换律的?当然不是..
我们先不去想为什么.如果我把上面的点号换成逗号试下.
复制代码 代码如下:
echo '1+5=' , 5+1; //输出 1+5=6
echo '1+5=' , 1+5; //输出 1+5=6
可以看出.只有使用逗号我们才可以得到意料中的结果.
那为什么点号就不行呢?逗号为什么就行呢?
复制代码 代码如下:
echo ('1+5' . 5)+1; //输出2
我们给前面的加个括号后.得到的结果是一样的.证明PHP是先连接字符串再进行加法计算了.按照从左向右的方向进行的.
那么好.既然是先连接的字符串.那么就应该是"1+55"了.然后再用这个字符串加上1.那为什么就会输出2呢?
这个跟PHP中字符串变成数字的机制是相关的.我们来看下面的例子
复制代码 代码如下:
echo (int)'abc1'; //输出0
echo (int)'1abc'; //输出1
echo (int)'2abc'; //输出2
echo (int)'22abc'; //输出22
从上面的例子我们可以看出.如果将一个字符串强制转换成一个数字.PHP会去搜索这个字符串的开头.如果开头是数字就转换.如果不是就直接返回0.
回到刚才的1+55.既然这个字符串是1+55.所以强制类型转换后就应该是1了.在此基础上加1.当然是2了.
为了证明我们的猜想.我们来验证一下.
复制代码 代码如下:
echo '5+1=' . 1+5; //输出10
echo '5+1=' . 5+1; //输出6
echo '1+5=' . 1+5; //输出6
echo '1+5=' . 5+1; //输出2
结果证明.我们的设想是正确的.
那么为什么使用逗号就没有上面的问题了呢?
手册上说了.用逗号是multiple parameters.
也就是说是多参数.换句话说.
逗号分隔开的就相当于是N个参数.也就是说把echo当个函数用.
这样的话.echo会对每个参数先进行计算.最后再进行连接后输出.所以我们用逗号就不存在上面的问题了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.
