PHP Functions
This page teaches you how to create your own functions.
A function is code that we can execute whenever we call on it to. All functions start with the word "function()" and the name you give the function should tell you roughly what the function does. The name can start with a letter or underscore but not a number. The function code is wrapped in brackets {}.Function
PHP Page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php function welcome() { echo "Welcome to my website."; } welcome(); ?> </body> </html> |
||
This is how it looks
|
Welcome to my website. |
||
Function with 1 parameter
Parameters, similar to varibles, can be added to functions to create a function with more functionality.PHP Page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php function welcome($name) { echo "Welcome to my website " . $name . ".<br>"; } welcome("Dave"); welcome("Brian"); welcome("Joe"); ?> </body> </html> |
||
This is how it looks
|
Welcome to my website Dave. Welcome to my website Brian. Welcome to my website Joe. |
||
Function with 2 parameters
If you have more than 1 parameter you just separate the parameters with commas.PHP Page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php function welcome($name, $lastname) { echo "Welcome to my website " . $name . " " . $lastname . ".<br>"; } welcome("Dave", "Thomas"); welcome("Brian", "Jones"); welcome("Joe", "Smith"); ?> </body> </html> |
||
This is how it looks
|
Welcome to my website Dave Thomas. Welcome to my website Brian Jones. Welcome to my website Joe Smith. |
||
Function that returns a value
A function can be used to return a value.PHP Page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php function add($wages,$capitalgains) { $income = $wages + $capitalgains; return $income; } echo "Your income is " . add(20000,10000) ?> </body> </html> |
||
This is how it looks
|
Your income is 30000 |
||
Bookmark this page: |








