logo

"We need a web site."

Now what?
When you have plenty of ideas, but not a lot of answers, the next step is WeavingtheWeb.com.

Enter your email address here to subscribe to the "PHP Tip of the Week," emailed to you every Monday morning. Enjoy!

 

PHP Tip of the Week Archive

  • If Brevity: The Ternary Operator
  • Easy Database Inserts
  • Output Buffering and Editing Web Pages On the Fly
  • Searching Arrays
  • Random Thoughts
  • Faster Page Transfer Using Compression
  • Persistent Values in Web Forms
  • Using HEREDOC Syntax with HTML
  • Calculating Dates More Easily
  • Using Single and Double Quotes
  • Timing Your PHP Scripts
  • When Nothing is Something: Zero, Empty, Null, and Negative Values
  • Nesting Instincts
  • Useful (for) Characters: The Increment Operator
  • The "static" keyword
  • Those !#@% Characters!
  • The Value of PHP Classes and OOP
  • The Simple Things in (programming) Life
  • Just a Passing Reference

    I’ve been looking through a lot of code written by others lately, and I’ve run across this scenario quite a bit:

    <?php

    function bumpit ($var ) {
      $var++;
      return $var;
    }

    $a = 5;
    $a = bumpit($a);
    print "Here is var a now: $a"; //$a is "6" now

    ?>

    That is, a function is sent the content of a variable ($a), the function modifies and returns that content, and the content is reassigned to the same variable.

    While that’s a perfectly allowed approach, it is not the most efficient approach, and it becomes less and less efficient the larger $a is to begin with.

    Why? Because the content of $a is actually copied into $var in this example. So we have two variables with exactly the same content (before $var is modified in bumpit( ) ). And that’s not terribly efficient. It would be better if we could modify $a directly within the function; we can, and here's one approach:

    <?php

    function bumpit(&$var) {
      $var++;
    }

    $a = 5;
    bumpit($a);
    print "Here is var a now: $a"; //$a is "6" now!

    ?>

    What happened to $a?

    First, it was passed into bumpit( ) by reference, indicated by the “&$varâ€. This means $var now points to the same content that $a does. $var is NOT a copy of $a.

    How does that work? We have a beloved family friend whose first name is Carol. However, we all know him as Grandpa Pinkie. Two names, same person.

    That’s an illustration of $a and $var in our second example above: two names, same content. Passing $a into bumpit( ) by reference does this.

    So when we modify $var within the function in that second example ($var++;), we are actually modifying the same content that $a points to. So $a is incremented when $var is incremented…because they both point to the same content. And we don’t even have to return $var from bumpit( ) in the second example. Just modifying $var within bumpit( ) modifies $a outside of bumpit( ).

    Again, if $a were a couple of megabytes of info to begin with, we’ve just saved ourselves the trouble of copying that content to another variable if we pass $a to a function by reference, like bumpit(&$var).

    A suggestion: It is easy to overlook that extra ampersand (&) in front of a variable name when passing it into a function. So comment your code so that you – and others after you – will be alerted to what you are doing when you pass by reference.

    Read more here:

    http://www.php.net/manual/en/language.references.pass.php

  • Simple Image Creation with PHP