Home  >  Article  >  Backend Development  >  Summary of PHP operator classification and use_PHP tutorial

Summary of PHP operator classification and use_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:28:051005browse

PHP operators are the most commonly used in our programs. I recently read a PHP book that introduced the use of PHP operators in depth. Let me share it with you here.

1: Arithmetic operator :
-: -$a negates the negative value of $a.
+: $a + $b adds the sum of $a and $b.
-: $a - $b subtracts the difference between $a and $b.
*: $a * $b multiplies the product of $a and $b.
/: $a / $b division The quotient of $a divided by $b.
%: $a % $b modulo the remainder of $a divided by $b.

Note: The division sign ("/") always returns a floating point number, even if both operands are integers (or integers converted from strings) modulo $a % $b in $a When the value is negative, the result is also negative.

Example:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span> </span></span></li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>php常量定义--阿涛随笔</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>a</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>3</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>b</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>4</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>c</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>5</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>d</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>6</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//$</SPAN><SPAN class=attribute><FONT color=#ff0000>d</FONT></SPAN><SPAN>=-$a;  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>e</FONT></SPAN><SPAN>=$a+$b;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>f</FONT></SPAN><SPAN>=$d-$c;  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>g</FONT></SPAN><SPAN>=$a*$d;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>h</FONT></SPAN><SPAN>=$d/$a;  </SPAN></SPAN><LI class=alt><SPAN>echo "{$e}</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>";  </span>
</li>
<li class="">
<span>echo "{$f}</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>";  </span>
</li>
<li class="alt">
<span>echo "{$g}</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>";  </span>
</li>
<li class="">
<span>echo "{$h}</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>";  </span>
</li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
</ol>

2: Assignment operator:

The basic assignment operator is "=". You might think it means "equal to" at first, but it's not. It actually means assigning the value of the right-hand expression to the left-hand operand.

1): Simple assignment:

<ol class="dp-xml"><li class="alt"><span><span>&</span><span class="attribute"><font color="#ff0000">a</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"http://www.admin300.com"</font></span><span>$</span><span class="attribute"><font color="#ff0000">b</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">2008</font></span><span> </span></span></li></ol>
3): Note: The assignment operation copies the value of the original variable to the new variable (assignment by value), so the change One does not affect the other. This is also suitable if you are copying some values ​​such as large values ​​in a tight loop. PHP 4 supports reference assignment, using the $var = &$othervar; syntax, but this is not possible in PHP 3. "Reference assignment" means that both variables point to the same data, and there is no copy of any data. PHP operator application tips: The value of the assignment operation expression is the assigned value. That is, the value of "$a = 3" is 3. This will allow you to do some tricks:
<ol class="dp-xml"><li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span> = ($</span><span class="attribute"><font color="#ff0000">b</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">4</font></span><span>) + 5;  </span></span></li></ol>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446484.htmlTechArticlePHP operators are the most commonly used in our programs. I recently read a PHP book that introduced it in depth. Let me share with you the use of PHP operators. 1: Arithmetic operators: ...
Statement:
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