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.

PHP Code:

<?php
function helloworld() {
//Code to run goes in here.
echo “Hello world.”;
}

helloworld(); //Runs the function.
?>

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.

PHP Code:

<?php
function multiply($num1, $num2) {
$answer = $num1 * $num2;
return $answer;
}

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.

For any kind of query or help, feel free to contact us at yeahhub@gmail.com

Credit Goes to Mr. RYDIOO

Related Posts