Cameron Garnett/Testpage9
Contents
Super Quick PHP Tutorial
Introduction | Basic Syntax | Sending Data to the Browser | Comments | Variables | Strings | Functions | Quotes | Debugging | Form
Learning ObjectivesBy the end of this page you will know what a function is and how to write them using php code. |
Functions
- A function is a block of statements that can be used repeatedly in a program.
- A function name can start with a letter or underscore (not a number).
- Give the function a name that reflects what the function does!
ExampleThis is a basic skeleton of a function. |
PHP has more than 1000 built-in functions. If you don't think that is enough then you can create your own.
User Defined Functions
To create a user defined function in PHP you need to start with the word function.
In this first example I will create a function called displaystring(). The first curly bracket indicates the start of the code and the second curly bracket indicates the end of the function. Very similar to javascript.
Example<?php |
In the next example to get a function to return a sum use the return statement. The $x and the $y inside the brackets are parameters.
Example<?php |
ActivityWrite the previous code into notepad++ and experiment with the format of functions. |