25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Functions ” 41<br />

$a = "Hello";<br />

$b = "World";<br />

function hello()<br />

{<br />

echo $GLOBALS[’a’] .’ ’. $GLOBALS[’b’];<br />

}<br />

hello(); // Displays "Hello World"<br />

Passing Arguments<br />

Arguments allow you to inject an arbitrary number of values into a function in order<br />

to influence its behaviour:<br />

function hello($who)<br />

{<br />

echo "Hello $who";<br />

}<br />

hello("World");<br />

/* Here we pass in the value, "World", and the function displays "Hello World"<br />

*/<br />

You can define any number of arguments and, in fact, you can pass an arbitrary number<br />

of arguments to a function, regardless of how many you specified in its declaration.<br />

<strong>PHP</strong> will not complain unless you provide fewer arguments than you declared.<br />

Additionally, you can make arguments optional by giving them a default value.<br />

Optional arguments must be right-most in the list and can only take simple values—expressions<br />

are not allowed:<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

function hello($who = "World")<br />

{<br />

echo "Hello $who";<br />

}<br />

hello();<br />

/* This time we pass in no argument and $who is assigned "World" by default. */

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!