一道小题引出的php数据类型转换问题
题目如下:
[php]
$a = 3;
$b = 5;
$c = $a > $b ? 2:4;
echo $d = $a
/*analysis why the result is 1*/
echo $d;
echo gettype($d);
echo var_dump($d);
/*example 2*/
if('ABC'==0)
echo 'yes';
else
echo 'no';
/*example 3 the result $h=10,$g=10;*/
$h=+$g=10;
echo $h,"$g";
/*这里关乎运算符优先级*/
?>
对结果感到疑惑吧!?
php作为一种弱类型语言,是允许不同类型的数据进行混合运算的(类似 VbScript),PHP 解释引擎自动做了类型转换的工作。必须引起注意。
先来回忆一下手册里的PHP echo() 函数
定义和用法
echo() 函数输出一个或多个字符串。
语法
echo(strings)
参数 描述
strings 必需。一个或多个要发送到输出的字符串。
注释:注释:echo() 实际上不是一个函数,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个或多个参数,那么使用括号会发生解析错误。echo()函数可以用成 echo('...'),也可以用成 echo '...'。echo 的操作数类型应该是字符串型的。
详细用法参见http://www.w3school.com.cn/php/func_string_echo.asp
为了更好的理解php数据类型转换,搞清楚计算结果的复杂诡异,请记住以下几点:
1:注意 echo 和 print 函数的区别,比如:
1)、
echo "123" . print("abc"); //语法正确,但输出不是123abc,而是 abc1231
//原因是首先执行 print()函数输出 abc,再连接"123"和函数的返回值1
print "abc" . echo('ooo'); //echo('ooo')会导致 Parse error: syntax error
2)、echo() 函数可以使用简化语法。 = '...' ?>
例如:
[php]
$color = "red";
?>
Roses are =$color?>
3)、echo()函数 比 print() 函数快一点点。
接下来,需要熟悉一下点运算符的操作,这里以网友“张庆(网眼)”的例子示意:
示例1:
最一般的情况,输出字符串,比如 echo 'abc' . "123",结果是 abc123,没说明好解释的。
示例2:
echo true;
输出结果是 1。true 是 PHP 内置布尔常量,输出前被转换为字符串"1"。
echo false;
输出结果是空。false 也是 PHP 内置布尔常量,输出前被转换为空字符串。
示例3:
echo 0=="ABC";
输出结果是 1。0=="ABC"中,字符串"ABC"首先被转换为数值型(字符串转换为数值,还有一个规则,有机会再讲),为0,再和 0 比较,结果为 true,再执行 echo true,为 1。默默地进行了 2 次类型转换。
再注意,echo 'ABC'==0; 结果还是 1。注意不是把 0 转换为字符串 "0" 再和字符串 'ABC' 比较从而得到 false。呵呵,很诡异吗?
类似地有另一个题目:
if('ABC'==0)
echo 'yes';
else
echo 'no';
知道到底输出 yes 还是 no 了吗?
示例4:再来个更诡异的。
echo 100 . "ABC"; // 注意!点符号两边都有空格
这句输出 100ABC,因为 100 被转化为"100"了。
echo 100."ABC"; // 注意!点符号两边没有空格,是连写
但这句报告语法错误!为什么呀?因为 PHP 在这里把点符号当小数点对待了,“100."ABC"” 当然不是个正确的数字了。
echo 100. "ABC";
只有点符号右边有空格,语法错误。原因如上。
echo 100 ."ABC";
只有点符号左边有空格,语法正确,输出 100ABC。
类似的:
$a = 100;
echo $a . 200; //点符号两边有空格
输出 100200。
$a = 100;
echo $a.200; //点符号两边没有空格
语法错误。“$a.200”也不是一个合格的变量吧。
示例5:
echo 100 + "ABC";
输出结果是 100。注意不要认为这会产生语法错误,因为这里的“+”号是算数运算符,不是连接运算符的误写。"ABC"被转换为数字 0 了,加 100 后等于100,再转换为字符串"100"输出来。进行了2次类型转换。
echo 100 + "20" . 30; //点符号两边有空格
结果输出 12030。
echo 100 + "20" .30; //点符号左边有空格,右边无空格
语法错误。.30 被当作 double 类型的 0.30 了。与 100 + "20" 的结果"120"直接摆在一起,PHP 分辨不清,报错了。
还可以举出一些例子来。仔细体会,还是能找出类型转换规律的,PHP 其实并不诡异。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
