Home >Backend Development >PHP Tutorial >How to determine PHP operator precedence_PHP tutorial

How to determine PHP operator precedence_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:27:491029browse

When learning PHP, you may encounter the problem of PHP operator priority. Here we will introduce the solution to the problem of PHP operator priority, and share it with you here. PHP supports an error control operator: @. When placed before a PHP expression, any error message that expression may produce is ignored. If the track_errors feature is activated, any error messages generated by the expression are stored in the variable $php_errormsg. This variable is overwritten on every error, so check it as early as possible if you want to use it.

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>my_file</FONT></SPAN><SPAN>=@file('non_existent_file')or  </SPAN></SPAN><LI class=alt><SPAN>die("Failedopeningfile:errorwas'$php_errormsg'");  </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=@$cache[$key];  </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>

Note: The @ operator is only valid for expressions. A simple PHP operator precedence rule for newbies is: if you can get a value from somewhere, you can precede it with the @ operator. For example, you can put it before variables, functions and include() calls, constants, etc. It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if and foreach. Execution operator: PHP supports an execution operator: backtick (``). Note that this is not a single quote! PHP will attempt to execute what is enclosed in the backticks as a shell command and return its output information (for example, it can be assigned to a variable rather than simply discarded to standard output). The effect of using the backtick operator "`" is the same as the function shell_exec().
<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>output</FONT></SPAN><SPAN>=`ls-al`;  </SPAN></SPAN><LI class=alt><SPAN>echo"</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>pre</SPAN><SPAN class=tag>></span></font></strong><span>$output</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>pre</SPAN><SPAN class=tag>></span></font></strong><span>";  </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

Note: The backtick operator is invalid when safe mode is activated or shell_exec() is turned off.

Add one/subtract one operator:
Add $a before ++$a, add one to the value of $a, and then return $a.
Add $a++ to return $a, and then increase the value of $a by one.
--Decrease the value of $a by one before $a, and then return $a.
$a--After subtracting, return $a, and then decrement the value of $a by one.

This is the same as self-increment and self-decrement in C language. Let’s take a simple example to deepen it

<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></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><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>a</FONT></SPAN><SPAN>=++$a;  </SPAN></SPAN><LI class=alt><SPAN>echo$a."</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> </span></li>
<li class="alt"><span>//输出值为4  </span></li>
<li class=""><span> </span></li>
<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>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span>=$a++;  </span>
</li>
<li class="alt"><span>echo$a;  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//输出值为3  </span></li>
<li class=""><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></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></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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446482.htmlTechArticleWhen learning PHP, you may encounter PHP operator priority issues. Here we will introduce PHP operator priority The solution to the problem is here to share with you. PHP support an error...
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