Home > Backend Development > PHP Tutorial > I don't understand the values ​​appearing in the PHP operation code. Please help explain.

I don't understand the values ​​appearing in the PHP operation code. Please help explain.

WBOY
Release: 2023-03-02 11:38:02
Original
1065 people have browsed it

header("Content-type:text/html;charset=utf-8");
$a = 10;
$b = '60';
echo "$a+$b=".$ a+$b."
";
echo "$a-$b=".$a-$b."
";
echo "$a$b=". $a$b."
";
echo "$a/$b=".$a/$b."
";
?>

The value output by the final web page is as follows:
70
-50
10*60=600
10/60=0.16666666666667

How did you get these values?

Reply content:

header("Content-type:text/html;charset=utf-8");
$a = 10;
$b = '60';
echo "$a+$b=".$ a+$b."
";
echo "$a-$b=".$a-$b."
";
echo "$a$b=". $a$b."
";
echo "$a/$b=".$a/$b."
";
?>

The value output by the final web page is as follows:
70
-50
10*60=600
10/60=0.16666666666667

How did you get these values?

echo "$a+$b=".$a+$b."
";Execution result (+-. is the execution order of the same level from left to right)

<code>"$a+$b=" => '10+60='
"$a+$b=".$a => '10+60=10'
("$a+$b=".$a) + $b => intval('10+60=10') + intval($b) => 10+60 =>70</code>
Copy after login

Click to view PHP operator precedence

$a is an integer, which is stored as long in php. $b is a string, which is forced to be converted to an integer when performing arithmetic operations. The decimal obtained by the division operation is stored as a double, and the rest is mathematical operations

Basic operations: addition, subtraction, multiplication and division

Here the string is automatically converted to an integer to participate in the operation

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template