PHP Order of operations

时光怂恿深爱的人放手 提交于 2020-01-06 12:46:10

问题


I wanted to know how PHP would execute this. Order of operations

addslashes(strip_tags($record['value']));

Is addslashes called first or strip_tags?

In other words, does it execute from the inside out or from the outside in?


回答1:


From the inside out.

The things passed into a function in PHP are called "expressions". When you pass an expression as a parameter, what you're really passing is the value of that expression. In order to do that, the expression is evaluated before it is passed in.

More about expressions from the php manual.




回答2:


strip_tags is called first.

and this is not just the case with PHP, it is the case with every other programming language (excluding some obscure esoteric language that may have some unique order of evaluation).

PS: Here is some documentation: PEDMAS. This is what inspired this kind of evaluation order in programming languages too.




回答3:


If you think about it in a logical way, what does PHP need in order to execute the function? The variable. So, strip_tags needs the $record['value'] to be inputted before it can execute the function and strip the tags from it. That function will return a value.

Now, addslahes needs a variable too. It cannot execute on a function, it needs that function inside it to return something for it to process. So it uses that returned value from strip_tags as its variable and executes upon that.




回答4:


addslashes takes one argument, in your case it is strip_tags($record['value']). addslashes can't be called when it's argument isn't resolved.

Therefore strip_tags must be called first. This is the case in nearly all popular programming languages out there. I wonder how you managed to get by before knowing this!



来源:https://stackoverflow.com/questions/2389422/php-order-of-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!