13.09.2016 Views

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

Create successful ePaper yourself

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

146 Chapter 5 Reusing Code <strong>and</strong> Writing Functions<br />

One other reason you may see this error message is that the function you are calling<br />

is part of a <strong>PHP</strong> extension that is not loaded. For example, if you try to use functions<br />

from the gd (image manipulation) library <strong>and</strong> you have not installed gd, you will see this<br />

message.<br />

Underst<strong>and</strong>ing Case <strong>and</strong> Function Names<br />

Note that calls to functions are not case sensitive, so calls to function_name(),<br />

Function_Name(), or FUNCTION_NAME() are all valid <strong>and</strong> all have the same result.You are<br />

free to capitalize in any way you find easy to read, but you should aim to be consistent.<br />

The convention used in this book, <strong>and</strong> most other <strong>PHP</strong> documentation, is to use all<br />

lowercase.<br />

It is important to note that function names behave differently to variable names.<br />

Variable names are case sensitive, so $Name <strong>and</strong> $name are two separate variables, but<br />

Name() <strong>and</strong> name() are the same function.<br />

Defining Your Own Functions<br />

In the preceding chapters, you saw many examples using some of <strong>PHP</strong>’s built-in functions.<br />

However, the real power of a programming language comes from being able to<br />

create your own functions.<br />

The functions built into <strong>PHP</strong> enable you to interact with files, use a database, create<br />

graphics, <strong>and</strong> connect to other servers. However, in your career, you often may need to<br />

do something that the language’s creators did not foresee.<br />

Fortunately, you are not limited to using the built-in functions; you can write your<br />

own to perform any task that you like.Your code will probably be a mixture of existing<br />

functions combined with your own logic to perform a task for you. If you are writing a<br />

block of code for a task that you are likely to want to reuse in a number of places in a<br />

script or in a number of scripts, you would be wise to declare that block as a function.<br />

Declaring a function allows you to use your own code in the same way as the built-in<br />

functions.You simply call your function <strong>and</strong> provide it with the necessary parameters.This<br />

means that you can call <strong>and</strong> reuse the same function many times throughout your script.<br />

Examining Basic Function Structure<br />

A function declaration creates or declares a new function.The declaration begins with the<br />

keyword function, provides the function name <strong>and</strong> parameters required, <strong>and</strong> contains<br />

the code that will be executed each time this function is called.<br />

Here is the declaration of a trivial function:<br />

function my_function() {<br />

echo ‘My function was called’;<br />

}

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

Saved successfully!

Ooh no, something went wrong!