- 我们无需做任何处理,而是,(程序会根据运算时运算符所需要的数据类型进行转换。
- 如果参与运算的数据不是需要的类型,则会自动转换为需要的数据类型。
- 转换为数字:
规律:一个字符串当做数字,就会将该字符串的最前面的数字转换为数字值,如果没有,就为0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
echo 1 + true;
echo 1 + false;
echo 1 + '3';
echo 1 . '3';
echo '1' + '2';
echo 1 + '2abc';
echo 1 + '2abc34';
echo 1 + 'abc';
echo 1 + 'abc2';
echo 1.2 + '2';
echo 1.2 + '2.2abc';
echo 1.2 + 'abc2.2';
echo '1.2abc' + 2;
echo '1.2abc' + '2abc';
?>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
echo 10.8 % 3.6;
echo '10.8 ' % '3.6';
echo '10.8 ' % '3.6abc';
echo '10.8abc ' % '3.6abc';
?>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$v1= (int)'1';
$v2= (float)'1.23';
$v3 = (string)$v1;
?>
</body>
</html>