Complete PHP Tutorial – Chapter 8

PHP Functions always plays an important role in programming while developing an application/site in PHP language.

User Defined Functions

A function is a block of code that needs to be used multiple times throughout your code. They aren’t executed upon loading of a script but instead when they are called upon. A user defined function starts with the term function followed by the name, brackets and curly brackets.

The code for the function goes inside the curly brackets. To call upon the function you use the name followed by brackets.

Function names must begin with a letter or an underscore but may contain special characters and numbers. They are not case sensitive.

Parameters

The brackets that come after the function name are used for parameters. They allow you to pass values through a function. When defining your function you must put the name that you wish to reference the variable by into the parentheses.

When calling your function you use this space for what you wish the parsed value to be. Multiple parameters can be used and are separated by commas. Let’s see an example where a function multiplies two numbers.

PHP Code:

<?php
function multiply($num1, $num2) {
echo $num1 * $num2;
}

multiply(3,12);
//Outputs 36.
?>

Return

A function can return a value using the return statement. The statement stops the function and send a value back to the calling code. Multiple values cannot be returned so if you wish to do this I would suggest storing the variables you wish to return anymore than 1 value. Let’s use our parameters example and add a return statement.

echo multiply(3,2);
//We can now use the function as if it were a variable.
?>

Predefined Variables

PHP comes with a wide collection of built in variables that allow you to do various things. These include things such as PHP Forms, GET, POST, $_COOKIE and a variety more. I feel these are a more intermediate level subject so I will be posting this thread with the following section containing links to the documentation for all the most important of these pre-defined variables.

For a complete list of all the pre-defined variables you should check the docs here.

List

I’d recommend having a look at all of these but only once you get your head around all of the more basics concepts of php.

$_SERVER

PHP Forms (Technicallly not a variable but still useful)

$_GET

$_POST

$_SESSION

$_COOKIE

$php_errormsg

What Next?

So you’ve reached the end of our Complete PHP Tutorial. My suggestion is that you find a project and stick with it, by doing this you’ll be able to build your knowledge to a greater extent but in a way it will stick with you.

Disclaimer: Yeahhub.com does not represent or endorse the accuracy or reliability of any information's, content or advertisements contained on, distributed through, or linked, downloaded or accessed from any of the services contained on this website, nor the quality of any products, information's or any other material displayed,purchased, or obtained by you as a result of an advertisement or any other information's or offer in or in connection with the services herein.