views:

141

answers:

4

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?

+9  A: 

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

JacobM
I was looking for some documentation on this as well, anything?BTW: Thanks :)
Phill Pafford
Edited to add more detail and a link.
JacobM
+5  A: 

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.

Here Be Wolves
A: 

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.

animuson
This is an example question, but thnx
Phill Pafford
A: 

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!

lamas