PHP basic operators, detailed examples of the difference between single quotes and double quotes

伊谢尔伦
Release: 2023-03-11 07:40:01
Original
1936 people have browsed it

one. StringInsert

The difference between double quotes and single quotes:
1. The use of double quotes:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<?php 
//双引号可以解析变量和转义字符 
$username = "jack"; 
echo "his name is $username!"; 
echo "<br/>"; 
$username = "小东"; 
//如果是英文的感叹号会正常解析变量 
echo "他的名字是$username!";//他的名字是小东! 
echo "<br/>"; 
//如果是中文的感叹号则会解析不出来 
echo "他的名字是$username!";//他的名字是 
echo "<br/>"; 
//转义字符在这里虽然被解析出来了,但是\n是在源代码里换行 
//浏览器显示只是一个字符的位置 
echo "他的名字是$username,\n他今年20岁了";//他的名字是小东, 他今年20岁了 
echo "<br/>"; 
//为了避免出现错误,推荐使用
字符串连接
的方式 
echo "他的名字是".$username.",他今年20岁了";//他的名字是小东,他今年20岁了 
?>
Copy after login

2. The use of single quotes:

<?php 
//单引号只是输出字符串字面值, 
//不会解析变量和转义字符。 
//也不会进行语法加亮提示 
$username = &#39;anllin&#39;; 
echo &#39;his name is $username,\n his age is 20.&#39;; 
//output 
//his name is $username,\n his age is 20. 
?>
Copy after login

Some commonly used escape characters

##\tHorizontal chart##\\##\$Operator

Escape sequence

Description

##\n

Line break

##\r

Enter

Backslash

Dollar sign

##\”

Double quotes

2.

##Instance one,

<?php 
//算术操作符 
$a = 5; 
$b = 3; 
echo $a + $b; 
echo &#39;<br/>&#39;; 
echo $a - $b; 
echo &#39;<br/>&#39;; 
echo $a * $b; 
echo &#39;<br/>&#39;; 
echo $a / $b; 
echo &#39;<br/>&#39;; 
echo $a % $b; 
?>
Copy after login
The results are as follows:
8 
2 
15 
1.66666666667 
2
Copy after login

Instance two,
<?php 
//复合赋值操作符 
$a = 5; 
$b = 3; 
echo $a += $b; 
echo &#39;<br/>&#39;; 
echo $a -= $b; 
echo &#39;<br/>&#39;; 
echo $a *= $b; 
echo &#39;<br/>&#39;; 
echo $a /= $b; 
echo &#39;<br/>&#39;; 
echo $a %= $b; 
echo &#39;<br/>&#39;; 
echo $a .= $b; 
?>
Copy after login

The results are as follows:


8 
5 
15 
5 
2 
23
Copy after login

Instance three ,

<?php 
//
递增递减运算符
 
$a = 5; 
echo ++$a; 
echo &#39;<br/>&#39;; 
echo $a++; 
echo &#39;<br/>&#39;; 
echo --$a; 
echo &#39;<br/>&#39;; 
echo $a--; 
?>
Copy after login

The results are as follows:

6 
6 
6 
6
Copy after login

Instance 4:

<?php 
$a = 5; 
$b = 3; 
$c = 5; 
$d = &#39;5&#39;; 
echo $a == $c; 
echo &#39;<br/>&#39;; 
echo $a === $c; 
echo &#39;<br/>&#39;; 
echo $a == $d; 
echo &#39;<br/>&#39;; 
echo $a != $b; 
echo &#39;<br/>&#39;; 
echo $a !== $d; 
echo &#39;<br/>&#39;; 
echo $a != $b; 
echo &#39;<br/>&#39;; 
echo $a > $b; 
echo &#39;<br/>&#39;; 
echo $b < $c; 
echo &#39;<br/>&#39;; 
echo $a >= $c; 
echo &#39;<br/>&#39;; 
echo $a <= $c; 
?>
Copy after login

The results are as follows:

1 
1 
1 
1 
1 
1 
1 
1 
1 
1
Copy after login

Example 5.

<?php 
$a = false; 
echo ! $a; 
echo &#39;<br/>&#39;; 
$b = 5; 
$c = 3; 
echo $b > 0 && $c > 0; 
echo &#39;<br/>&#39;; 
echo $b > 0 and $c > 0; 
echo &#39;<br/>&#39;; 
echo $b != 0 || $c != 0; 
echo &#39;<br/>&#39;; 
echo $b != 0 or $c != 0; 
echo &#39;<br/>&#39;; 
?>
Copy after login

The results are as follows:

1 
1 
1 
1 
1
Copy after login

The operators "and" and "or" have a

priority

higher than && and || Low
ternary operator

<?php 
$a = 100; 
echo $a > 60 ? &#39;success&#39;:&#39;fail&#39;; 
?>
Copy after login

success
error suppression operator

<?php 
echo @(100/0); 
?>
Copy after login



The above is the detailed content of PHP basic operators, detailed examples of the difference between single quotes and double quotes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!