<p>In the previous article, I brought you "<a href="https://www.php.cn/php-ask-482943.html" target="_blank">Technical Answers: How to View and Judge PHP Data Types (Learning Sharing)</a>". Today I will continue to bring you an explanation of PHP knowledge and give you Let’s introduce the relevant knowledge of PHP operators in detail. Hope it helps everyone! </p>
<p><img src="https://img.php.cn/upload/article/000/000/067/61679c8edab70581.jpg" alt="Nanny-level sharing of basic PHP operators" ></p>
<h2>PHP Operator</h2>
<p>Operator refers to certain symbols that generate another value through one or more expressions. There are many kinds of operators. , such as: "<code> </code>", "<code>%</code>", "<code>.</code>", etc. are all operators. So let’s take a look at the applications of different operators in PHP. <br></p>
<ul style="list-style-type: disc;"><li><p><strong>PHP Arithmetic Operator</strong></p></li></ul>
<ul style="list-style-type: circle;">
<li><p>“<code> </code> "Addition operation, for example: $a $b;</p></li>
<li><p>"<code>-</code>"Subtraction operation, for example: $a-$b;</p></li>
<li><p>"<code>*</code>"Multiplication operation, for example: $a*$b;</p></li>
<li><p>"<code>/</code>"Division operation, for example :$a/$b;</p></li>
<li><p>“<code>%</code>” takes the remainder operation (modulo operation), for example: $a%$b;</p></li>
</ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$x=3;
$y=4;
echo ($x + $y)."<br/>"; // 输出 7
echo ($x - $y)."<br/>"; // 输出 -1
echo ($x * $y)."<br/>"; // 输出 12
echo ($x / $y)."<br/>"; // 输出 0.75
echo ($x % $y)."<br/>"; // 输出 3
?></pre><div class="contentsignin">Copy after login</div></div><p>Output result: </p><p><img src="https://img.php.cn/upload/image/150/240/256/1634175944842598.png" title="1634175944842598.png" alt="Nanny-level sharing of basic PHP operators"/></p><p>It can be concluded that using different arithmetic operators gives different results. <br/></p><ul style="max-width:90%"><li><p><strong>PHP assignment operator</strong></p></li></ul><p>We call = (equal sign) in the PHP operator As an assignment operator, it is used to assign values to variables, that is, assign the value on the right side of the equal sign to the variable on the left side of the equal sign, and the variable on the left side becomes the value on the right side. </p><ul style="list-style-type: circle;"><li><p>"<code> =</code>"Addition example: $x = $y , that is: $x = $x $y , add the variable on the left side of the operator to the right The value is assigned to the variable on the left. </p></li><li><p>"<code>-=</code>" Subtraction example: $x -= $y , that is: $x = $x - $y , change the variable on the left side of the operator Subtract the value on the right and assign it to the variable on the left. </p></li><li><p>" <code>*=</code>"Multiplication example: $x *= $y , that is: $x = $x * $y , change the variable on the left side of the operator Multiply the value on the right and assign it to the variable on the left. </p></li><li><p>"<code>/=</code>" Division example: $x /= $y , that is: $x = $x / $y , change the variable on the left side of the operator Divide the value on the right and assign it to the variable on the left. </p></li><li><p>"<code>%=</code>" Example of remainder: $x %= $y , that is: $x = $x % $y , change the left side of the operator The variable is evaluated modulo the value on the right and the result is assigned to the variable on the left. </p></li><li><p>"<code>.=</code>" splicing example: $x .= $y , that is: $x = $x . $y , append the characters on the right to left. </p></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$a = 3;
$b = 4;
echo ($a += $b).'<br>';//$a += $b 的值为7
echo ($a -= $b).'<br>';//$a -= $b 的值为3
echo ($a *= $b).'<br>';//$a *= $b 的值为12
echo ($a /= $b).'<br>';//$a /= $b 的值为3
?></pre><div class="contentsignin">Copy after login</div></div><p>Output result: <br/></p><p><img src="https://img.php.cn/upload/image/394/391/289/1634177475609715.png" title="1634177475609715.png" alt="Nanny-level sharing of basic PHP operators"/></p><p>It can be seen that: $x = $y is equivalent to $x = $ x $y.</p><ul style="max-width:90%"><li><p><strong>PHP string operator</strong></p></li></ul><p>There is only one string operator, which is the English period "<code>.</code>", it is to connect two strings and splice them into a new string. </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$a = 'PHP';
$b = '中文网';
$c = $a.$b;//通过字符串运算符将两个字符串连接起来
echo $c;
?></pre><div class="contentsignin">Copy after login</div></div><p>Output result: <br/></p><p><img src="https://img.php.cn/upload/image/579/474/600/1634177873659557.png" title="1634177873659557.png" alt="Nanny-level sharing of basic PHP operators"/></p><ul style="max-width:90%"><li><p>##PHP increment/decrement operator<strong></strong></p></li></ul>In our daily use, arithmetic operators are suitable for use when there are two or more different operands, but when there is only one operand, you can use increment "<p> <code>" or decrement "</code>--<code>" operator. </code></p><ul style="list-style-type: circle;"><li>First increase or decrease the variable by 1 and then assign the value to the original variable, which is called the prefix increment or decrement operator (prefix auto-increment and self-decrement operator);<p></p></li><li>Put the operator after the variable, that is, first return the current value of the variable, and then increase or decrease the value of the variable by 1, which is called a post-increment or decrement operator ( Post-increment and decrement operators). <p></p></li><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$a = 3;
$b = 4;
$c = 5;
$d = 6;
echo ++$a.'<br>';//输出结果为4
echo $b++.'<br>';//输出结果为4
echo --$c.'<br>';//输出结果为4
echo $d--.'<br>';//输出结果为6
?></pre><div class="contentsignin">Copy after login</div></div></ul>Output result: <p><br/></p><p><img src="https://img.php.cn/upload/image/773/788/524/1634178360606790.png" title="1634178360606790.png" alt="Nanny-level sharing of basic PHP operators"/></p><ul style="max-width:90%"><li>##PHP comparison operator<p> <strong></strong></p></li>PHP comparison operators are used to compare two values (numbers or strings) </ul><p></p><ul style="list-style-type: circle;"><li>==<p> are used to Comparison is equal to, for example: $x == $y , if $x is equal to $y, return true. <code></li><li><p><code>===</code>用于比较全等(完全相同),例:$x === $y , 如果 $x 等于 $y,且它们类型相同,则返回 true </p></li><li><p><code>!=</code> 用于比较不等于 ,例: $x != $y , 如果 $x 不等于 $y,则返回 true。 </p></li><li><p><code><> </code> 用于比较不等于,例: $x <> $y , 如果 $x 不等于 $y,则返回 true。 </p></li><li><p><code>!==</code>用于比较不全等(完全不同),例:$x !== $y , 如果 $x 不等于 $y,且它们类型不相同,则返回 true。 </p></li><li><p><code>></code> 用于比较大于,例:$x > $y , 如果 $x 大于 $y,则返回 true。 </p></li><li><p><code><</code> 用于比较小于,例: $x < $y , 如果 $x 小于 $y,则返回 true。 </p></li><li><p><code>>=</code> 用于比较大于或等于 ,例: $x >= $y , 如果 $x 大于或者等于 $y,则返回 true. </p></li><li><p><code><=</code> 用于比较小于或等于 ,例: $x <= $y , 如果 $x 小于或者等于 $y,则返回 true。 </p></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$x=10086;
$y="10086";
var_dump($x == $y);
echo "<br>";
var_dump($x === $y);
echo "<br>";
var_dump($x != $y);
echo "<br>";
var_dump($x !== $y);
echo "<br>";
$a=50;
$b=100;
var_dump($a > $b);
echo "<br>";
var_dump($a < $b);
?></pre><div class="contentsignin">Copy after login</div></div><p>输出结果:<br/></p><p><img src="https://img.php.cn/upload/image/561/559/770/1634179033792144.png" title="1634179033792144.png" alt="Nanny-level sharing of basic PHP operators"/></p><ul style="max-width:90%"><li><p><strong>PHP逻辑运算符</strong></p></li></ul><ul style="list-style-type: circle;"><li><p><code>&&</code>与逻辑运算符,例:$x && $y , 如果 $x 和 $y 都为 true,则返回 true。 <br/></p></li><li><p><code>||</code>或逻辑运算符,例:$x || $y, 如果 $x 和 $y 至少有一个为 true,则返回 true。 </p></li><li><p><code>! </code>非逻辑运算符,例:!$x , 如果 $x 不为 true,则返回 true。 </p></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
header("Content-type:text/html;charset=utf-8");
$x = true;
$y = false;
//逻辑与(并且),要求两个都为true才执行真区间,所以代码中执行假区间
if($x && $y){
echo '好好学习';
}else{
echo '天天向上';
}
?></pre><div class="contentsignin">Copy after login</div></div><p>输出结果:</p><p><img src="https://img.php.cn/upload/image/292/837/299/1634179520261662.png" title="1634179520261662.png" alt="Nanny-level sharing of basic PHP operators"/></p><p>输出天天向上,所以刚才输出了假区间。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
header("Content-type:text/html;charset=utf-8");
$foo = false;
$bar = true;
//逻辑或,有一个为真则为真
if($foo || $bar){
echo '福如东海';
}else{
echo '寿比南山';
}
?></pre><div class="contentsignin">Copy after login</div></div><p>输出结果:<br/></p><p><img src="https://img.php.cn/upload/image/781/254/523/1634179697918544.png" title="1634179697918544.png" alt="Nanny-level sharing of basic PHP operators"/></p><p>输出福如东海,逻辑或,有一个为真则为真,所以输出了真区间。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
header("Content-type:text/html;charset=utf-8");
$foo = false;
//逻辑非,把false变为了true
if(!$foo){
echo '好好学习';
}else{
echo '天天向上';
}
?></pre><div class="contentsignin">Copy after login</div></div><p>输出结果:</p>
<p><img src="https://img.php.cn/upload/image/819/382/451/1634179864228803.png" title="1634179864228803.png" alt="Nanny-level sharing of basic PHP operators"></p>
<p>输出好好学习,逻辑非,把flase变成了true,输出了真。</p>
<p>推荐学习:《<a href="https://www.php.cn/course/list/29/type/2.html" target="_blank">PHP视频教程</a>》</p>
The above is the detailed content of Nanny-level sharing of basic PHP operators. For more information, please follow other related articles on the PHP Chinese website!