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>
<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>