Day 3 – Functions

Zend PHP Certification

This is a short chapter on custom functions, so this will therefore be a short post.  As you know, functions are the bread-and-butter of PHP code as they allow you to encapsulate repeatable code in a reusable manner, making the transition away from spaghetti code

  • Just like variable names, function names must only contain letters, numbers, and underscores while not starting with a number.

Return Values

  • Funny thing about functions in PHP, they ALWAYS return something (NULL by default), there is no void (only ZUUL).
  • Functions can be forced to return by reference by placing an ampersand (&) before the function name.
    function &retRef() { }
    1. </li>
    2. </ul>
    3. <h3>Functional Scope</h3>
    4. <ul>
    5. <li>All variables created in a function are destroyed upon exiting. </li>
    6. <li>Variables declared outside of a function are not accessible inside unless passed by parameters or through use of the Global scope. </li>
    7. <li><strong>Global scope</strong> (used through the global command or $GLOBALS superglobal array) allows variables to be read and modified inside a function without the need to pass as parameters.</li>
    8. </ul>
    9. <h3>Arguments</h3>
    10. <ul>
    11. <li>PHP will only yell at you if you provide<strong> too few arguments</strong> to a function.&#160; In theory, you can provide as many arguments as you want as long as it is equal to or more than the arguments declared by the function.
    12. <pre lang="php">function simple($one, $two) {}
    13.  
    14. simple($a, $b, $c, $d, $e);
  • Arguments can be set as optional by providing a default value.
    function optArguments($required, $optional = false) {}
    1. </li>
    2. <li>If you want a <strong>variable number of arguments,</strong> there are three functions designed to handle that.
    3. <ul>
    4. <li><strong>func_num_args():</strong> returns the number of arguments that were passed. </li>
    5. <li><strong>func_get_arg($num):</strong> returns the value of the $num argument. </li>
    6. <li><strong>func_get_args():</strong> returns an array of all the arguments. </li>
    7. </ul>
    8. <p>Here is an example: </p>
    9. <pre lang="php">function varArgs() {
    10.   if (func_num_args() == 0)
    11.     return "Please provide at least ONE argument";
    12.   $args = func_get_args();
    13.   foreach ($args as $arg)
    14.     echo $arg."\n";
    15. }
  • Arguments can also be forced to be passed by reference by preceding them with the ampersand (&).
    function passByRef(&$myArg) {
    1.   $myArg = 42;
    2. }

Well, that was it for this chapter.  Simple and straight to the point.  Tomorrow I start chapter 3, Arrays.  Woohoo!

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.