|
PHP Tutorial Home
Basic PHPPHP IntroductionPHP Installation PHP Syntax PHP Variables PHP Form Variables PHP If Statements PHP Switch Statements PHP Arrays PHP While Loops PHP For Loops PHP Operators PHP Functions Advanced PHPPHP IncludePHP Upload File PHP File PHP Mail PHP Mail Configuration PHP Database Driven Website PHP Summary FREE Hosting!With every domain name you register with ZappyHost, you get FREE hosting.$1.99 Domain NamesWith every new non-domain purchase thru ZappyHost, you get a domain name for only $1.99. |
PHP FunctionsSometimes you might find yourself writing the same piece of code over and over again. You might even find yourself "copy/pasting" code so that you can re-use it in another part of your application. The problem with doing this is, you now have more code to maintain. For example, if you want to change the code, you need to change it in many different places. A better idea would be to put that code into a function. PHP functions are self contained blocks of code that perform a specified "function". A function often accepts one or more "parameters" (also referred to as "arguments") which you can "pass" to it. By providing a different parameter to the function, you get a different result (depending on what the function actually does). Creating PHP FunctionsTo create a PHP function, follow these steps:
Example
Calling PHP FunctionsThe function doesn't actually do anything until you call it. To call a function, do the following:
ExampleIn this example, we create the same function we created in the previous example. We then call that function:
The above code results in the following:
My favorite fruit is pomegranate.
Passing ParametersThe previous example is a very simplistic version of a function. We could improve this function by allowing it to accept a parameter. For example, imagine if, at the time we called the function, we could pass it the name of a fruit. The function could then display the name of that fruit (instead of displaying "pomegranate" every time). Example 1
The above code results in the following:
My favorite fruit is: Watermelon
This is one of the things that make functions so cool. For example, by accepting a parameter (such as the fruit name), we could let our website users provide us with the name of their favorite fruit. Behind the scenes, we could put that fruit into a variable, then pass that variable to our function. The function could then display the name of their favorite fruit (instead of "pomegranate"!). Example 2In this example, the fruit name is supplied as a variable. For the purposes of this tutorial, we set the variable to a hard-coded string, but there's no reason this couldn't be a value provided by the user (for example, a value from a form).
The above code results in the following:
My favorite fruit is: Watermelon
Return ValuesIn the previous examples, our functions simply displayed a string to the user. Sometimes, you might not want your function to automatically display the result. Sometimes you might just want the function to return the result to you so that you can use it for your own programmatical purposes. That way, it's your choice whether you display it to the user, or do something else with the result. To create a return value, you type return followed by the value you want to return. Example
The above code results in the following:
104
Enjoy this website?
Oh, and thank you for supporting Quackit! |
Need Content for your Website?Get unique, quality digital content for your website. You can even earn money by reselling it!Includes:
|