• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/15

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

15 Cards in this Set

  • Front
  • Back
  • 3rd side (hint)
What is a function
A piece of code that has the ability to be called over and over again.
Page 37
How do you create a new function in PHP?
Use the keyword function, followed by an identifier.

Example:

function myFunction()
{
}
Page 37
Are PHP functions case sensitive?
No.
Page 37
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.
Page 37
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.
Page 38
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.
P 38
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:

fuction &query($sql)
{
$result = mysql_query($sql);
return $result;
}

Caveats:
You must return a variable. If you return an expression or empty return statement.
P 39
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
P 39 - 40
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"
P 40 - 41
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"
P 41
What is a variable-length argument list in PHP?
It is the ability to create a function that accepts a variable number of arguments.
P 42
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
P 42 - 43
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"
P43 - 44
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.
P 44
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;
}
P 44