Functions allow you to write lines of code that can be used again and again in different parts of a program without actually having to write the lines of code several times. You can create a function that runs some lines of code (usually a specific set of instructions) and then ‘call’ that function to be used later on.
In the PHP language (like all other modern languages) there are a range of built-in functions that you can use in your programs. Many functions exist such as math functions (eg. to round a number), date functions (eg. to display the current time and date), and string functions (eg. to count how many characters are in a string, or convert a string to lowercase/uppercase). However, you can make your own functions and use them in your program.
Watch the video below to see how to create functions without parameters, with a parameter, and with more than one parameter. Then scroll down to see the sample PHP code.
Sample PHP code:
1 2 3 4 5 6 7 8 | <?php function add($a,$b){ $c = $a + $b; echo $a , " + " , $b , " = " , $c; } add(5,10); ?> |
PHP Manual references: