2

Is there a principle that advises against doing things like:

mysql_real_escape_string(trim(str_replace($var, "black", "<body text='%body%'>")));

If something like that should be a no-no, then would this be acceptable?

$var = trim($var);
$var = str_replace($var, "black", "<body text='%body%'>");
$var = mysql_real_escape_string($var);

Or is that bad practice also, to call and perform on the same variable on a single like, like I did above? Would it be better to do:

$var1 = trim($var);
$var2 = str_replace($var1, "black", "<body text='%body%'>");
$var3 = mysql_real_escape_string($var2);

I've always wondered this!

5 Answers 5

4

I think the normal practice is to "nest" the functions like the first example.

There are a couple reasons, but mostly it shows that everything is happening to the same object in a specific order, with nothing going on in-between.

That being said, if you don't know exactly what you are going to be doing, you might want to start with the second example, so you can easily go back and add in functions.

Basically, the first is preferred and more common (I think) in the end, the second is good for testing and development, and the third is just a waste of resources (granted it is small, but there's no need).

Sign up to request clarification or add additional context in comments.

1 Comment

All of these answers are helpful but I'll mark this one for its clarity. Thanks!
4

Nesting functions is fine - as long as you take readability into account. In this fairly simple example there's a great deal of mental effort that needs to be put into parsing the statement:

mysql_real_escape_string(trim(str_replace($var, "black", "<body text='%body%'>")));

If you decide to use nested functions, also make use of intelligent indentation to make it easier to see what's going on:

mysql_real_escape_string(
  trim(
    str_replace(
       $var, 
       "black", 
       "<body text='%body%'>"
)));
  • Each function argument is on it's own indented line (trim and str_replace are both being used as function arguments, as are $var, "black" and "<body text='%body%'>")
  • The ending ))); clearly delimits where the function nesting ends. It also provides a quick syntax checksum (count the number of opening parentheses and make sure they match the number of closing parentheses at te end).

1 Comment

+1 for the indentation idea. I have never thought of that, but it makes a lot of sense.
2

In my opinion making code simple and readable goes a long way. I really don't like it when I have to read code like your first example. Usually by someone trying to show how complex they can make something.

I think either way you have it there is better than the first example.

1 Comment

Agree with this 100%. If I nest it's no more than 2 deep and obvious what's being done.
2

All of your examples are fine; there's not really much difference between them. If you want to make your first example a little more readable, you can indent it:

mysql_real_escape_string(
  trim(
    str_replace($var, "black", "<body text='%body%'>")));

Assigning the partial results to temporary variables, like in your second and third examples, is fine too. But it might be more useful if you give the variables meaningful names:

$trimmed = trim($text);
$html = str_replace($trimmed, "black", "<body text='%body%'>");
$sql = mysql_real_escape_string($html);

BTW, your first example calls the functions in a different order than the others.

1 Comment

+1 for mentioning the different order. Hopefully the OP is aware of this.
1

I agree with both sloopjohnB and Jon, in different respects. I believe the first example is perfectly valid, and usually common practice. If it's code that others are going to read and work on, then it just may seem to some that you are trying to show your coding savvy by doing that but it is normal practice and if the code is just for you to view and work on, then by all mean, use it if it's comfortable for you.

For me personally, I like the second option better. Gives you a more clear view of what's going on and in what steps you are performing it. Also gives you the option to add or change functions, in it's proper location/sequence, much easier. If you need to add a function to the first example, you could easily forget a bracket or put the function or bracket in the wrong location, really throwing you off.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.