Creating Your First PHP Function
When you make a function, you first need to give it a name, like my Company It’s with this function name that you will be able to call upon your function, so make it easy to type and understand.
The actual syntax for creating a function is pretty self-explanatory, but you can be the judge of that. First, you must tell PHP that you want to create a function. You do this by typing the keyword function followed by your function name and some other stuff (which we’ll talk about later).
Here is how you would make a function called my company . Note: We still have to fill in the code for my Company Motto.
PHP Code:
<?php
function mycompany(){
}
?>
Note: Your function name can start with a letter or underscore “_”, but not a number!
With a properly formatted function in place, we can now fill in the code that we want our function to execute. Do you see the curly braces in the above example “{ }”? These braces define where our function’s code goes. The opening curly brace “{” tells php that the function’s code is starting and a closing curly brace “}” tells PHP that our function is done!
We want our function to print out the company motto each time it’s called, so that sounds like it’s a job for the echo function!
PHP Code:
<?php
function mycompany(){
echo “We deliver quantity, not quality!<br />”;
}
?>
That’s it! You have written your first PHP function from scratch! Notice that the code that appears within a function is just the same as any other PHP code.
Using Your PHP Function
Now that you have completed coding your PHP function, it’s time to put it through a test run. Below is a simple PHP script. Let’s do two things: add the function code to it and use the function twice.
PHP Code:
<?php
echo “Welcome to Phpguru.com <br />”;
echo “Well, thanks for stopping by! <br />”;
echo “and remember… <br />”;
?>
PHP Code with Function:
<?php
function mycompany(){
echo “We deliver quantity, not quality!<br />”;
}
echo “Welcome to phpguru.com <br />”;
mycompany();
echo “Well, thanks for stopping by! <br />”;
echo “and remember… <br />”;
mycompany();
?>
Display:
Welcome to Phpguru.com
We deliver quantity, not quality!
Well, thanks for stopping by!
and remember…
We deliver quantity, not quality!
Although this was a simple example, it’s important to understand that there is a lot going on and there are a lot of areas to make errors. When you are creating a function, follow these simple guidelines:
* Always start your function with the keyword function
* Remember that your function’s code must be between the “{” and the “}”
* When you are using your function, be sure you spell the function name correctly
* Don’t give up!
PHP Functions – Parameters
Another helpful thing about functions is that you can send them information that the function can then use. Our first function mycompany isn’t all that useful because all it does, and ever will do, is print out a single, unchanging string.
However, if we were to use parameters, then we would be able to add some extra functionality! A parameter appears with the parentheses “( )” and looks just like a normal PHP variable. Let’s create a new function that creates a custom greeting based off of a person’s name.
Our parameter will be the person’s name and our function will concatenate this name onto a greeting string. Here’s what the code would look like.
PHP Code with Function:
<?php
function myGreeting($firstName){
echo “Hello there “. $firstName . “!<br />”;
}
?>
When we use our myGreeting function we have to send it a string containing someone’s name, otherwise it will break. When you add parameters, you also add more responsibility to you, the programmer! Let’s call our new function a few times with some common first names.
PHP Code:
<?php
function myGreeting($firstName){
echo “Hello there “. $firstName . “!<br />”;
}
myGreeting(“Dan”);
myGreeting(“Ros”);
myGreeting(“Sam”);
myGreeting(“Madi”);
?>
Display:
Hello there Dan!
Hello there Ros!
Hello there Sam!
Hello there Madi!
It is also possible to have multiple parameters in a function. To separate multiple parameters PHP uses a comma “,”. Let’s modify our function to also include last names.
Apart from the annoyance of superfluous typing, there is a danger of getting lazy, seeing that <?php echo $foo;?> is remarkably shorter to type. In some situations, it won’t manifest itself as a problem either, since some content-types never contains HTML special characters (Numbers for example). This is particularly nasty, because errors in the view layer are notoriously hard to track down.
The Clash :
There is a problem though; Since this is such a good name for a function, chances are that someone else would use it for something different, or perhaps even for the same. I already know of at least one potential nameclash, since I got the idea from CakePHP.
The usual way of dealing with this, would be with namespaces, but — alas — PHP doesn’t have namespaces, and the common solution of pseudo namespaces(Eg. prefixing names) doesn’t work here, since it would defy the purpose of the function in the first place.