13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Functions in <strong>PHP</strong><br />

143<br />

Note that the syntax is slightly different from the same option in php.ini: As well as<br />

php_value at the start of the line, there is no equal sign. A number of other php.ini<br />

configuration settings can be altered in this way, too.<br />

Setting options in the .htaccess file rather than in either php.ini or your web server’s<br />

configuration file gives you a lot of flexibility.You can alter settings on a shared<br />

machine that affect only your directories.You do not need to restart the web server, <strong>and</strong><br />

you do not need administrator access. A drawback to the .htaccess method is that the<br />

files are read <strong>and</strong> parsed each time a file in that directory is requested rather than just<br />

once at startup, so there is a performance penalty.<br />

Using Functions in <strong>PHP</strong><br />

Functions exist in most programming languages; they separate code that performs a single,<br />

well-defined task.This makes the code easier to read <strong>and</strong> allows you to reuse the<br />

code each time you need to perform the same task.<br />

A function is a self-contained module of code that prescribes a calling interface, performs<br />

some task, <strong>and</strong> optionally returns a result.<br />

You have seen a number of functions already. In preceding chapters, we routinely<br />

called a number of the functions built into <strong>PHP</strong>.We also wrote a few simple functions<br />

but glossed over the details. In the following sections, we cover calling <strong>and</strong> writing functions<br />

in more detail.<br />

Calling Functions<br />

The following line is the simplest possible call to a function:<br />

function_name();<br />

This line calls a function named function_name that does not require parameters.This<br />

line of code ignores any value that might be returned by this function.<br />

A number of functions are called in exactly this way.The function phpinfo() is often<br />

useful in testing because it displays the installed version of <strong>PHP</strong>, information about <strong>PHP</strong>,<br />

the web server setup, <strong>and</strong> the values of various <strong>PHP</strong> <strong>and</strong> server variables.This function<br />

does not take any parameters, <strong>and</strong> you generally ignore its return value, so a call to<br />

phpinfo() is written as follows:<br />

phpinfo();<br />

Most functions, however, do require one or more parameters, which are the inputs to<br />

functions.You pass parameters by placing data or the name of a variable holding data<br />

inside parentheses after the function name.You could call a function that accepts a single<br />

parameter as follows:<br />

function_name(‘parameter’);

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

Saved successfully!

Ooh no, something went wrong!