keywords:
Bookmark and Share



Front Back
What is a function
  • A piece of code that has the ability to be called over and over again.
How do you create a new function in PHP?
  • Use the keyword function, followed by an identifier.
  • Example:
    function myFunction()
    {
    }
Are PHP functions case sensitive?
  • No.
What are the naming rules for a PHP function?
  • Name must consist of only letters (a-z), numbers and the underscore character. It cannot start with a number.
What does the "return" keyword do in a fuction?
  • Returns a value and exits the function immediately, The rest of the code in the function is ignored.
If the "return" keyword is not used in a function, is a value returned?
  • Yes. If you do not return a value using the "return" keyword, then the function will return NULL when it completes. The concept of "void" functions does not really apply to PHP.
Can you return a function by reference in PHP? If so, how?
  • Yes. You can prepend the "&" to the function name to return function by reference. This is typically used to return a resource, such as a database connection, or implementing the Factory design pattern.
  • Example:
    function &query($sql)
    {
    $result = mysql_query($sql);
    return $result;
    }
  • Caveats:
    You must return a variable. If you return an expression or empty return statement.

What are the three variable scopes in PHP?
  • Global: available to all parts of the script
  • Function: available only inside the function
  • Class: available only inside a class
How do you access global variables inside a function in PHP?
  • There are two ways. First, you can use the "global" keyword.
  • Example:
    $a = "Hello";
    $b = "World";
    function hello()
    {
    global $a, b$;
    echo "$a $b";
    }
    hello(); //Displays "Hello World"
  • Second, use the $GLOBALS superglobal array.
  • Example:
    $a = "Hello";
    $b = "World";
    function hello()
    {
    echo $GLOBALS['a'].' '.$GLOBALS['b'];
    }
    hello(); //Displays "Hello World"
How do you make arguments optional in a PHP function?
  • To make arguments optional in PHP, you need to give them a default value in the function. Optional arguments must be right most in the list and take simple values, expressions are not allowed.
  • Example:
    function hello($who = "World")
    {
    echo "Hello $who";
    }
    hello(); //display "Hello Word"
    hello("Ted"); display "Hello Ted"
What is a variable-length argument list in PHP?
  • It is the ability to create a function that accepts a variable number of arguments.
What are the three built-in functions in PHP to handle variable-length argument lists?
  • func_num_args(): Return the total number of arguments passed into the function, declared or anonymous
  • func_get_arg(int): Return the argument at position int
  • func_get_args(): Return an array of all the arguments passed into the function
How are arguments passed to a function by reference in PHP?
  • Prefix the argument variable name with the by-reference operator "&".
  • Example:
    $name = "Sam";
    echo $name; //Output "Sam"
    function setName(&$name)
    {
    $name = "Ted";
    }
    setName($name);
    echo $name; //displays "Ted"
Can you pass an expression into a function by reference?
  • No, only variables can be passed in by reference. If you attempt to pass in an expression by reference, you will get a fatal error.
Can you create an optional parameter declared as by-reference in a function?
  • Yes, but only in PHP 5 and above.
  • Example:
    function setName(&$name = "Tim")
    {
    echo $name;
    }
x of y cards