1<?php
2function writeMsg() {
3 echo "Hello world!";
4}
5
6writeMsg(); //call the function
7?>
1<html>
2
3 <head>
4 <title>Writing PHP Function with Parameters</title>
5 </head>
6
7 <body>
8
9 <?php
10 function addFunction($num1, $num2) {
11 $sum = $num1 + $num2;
12 echo "Sum of the two numbers is : $sum";
13 }
14
15 addFunction(10, 20);
16 ?>
17
18 </body>
19</html>
1// functions require 'function' keyword
2// separate the parameters with a comma
3function addFunction($num1, $num2) {
4 $sum = $num1 + $num2;
5 echo "Sum of the two numbers is : $sum";
6}
7
8addFunction(1, 2); // echoes: Sum of the two numbers is : 3
1#functions
2
3<?php
4 #function - a block of code that can be repeatedly called
5
6 /*
7 How to format functions
8 1. Camel Case myFunction()
9 2.Lower case with underscore my_function()
10 3. Pascal Cae - MyFunction() usally used with classes
11 */
12 function simpleFunction(){
13 echo 'Hello John';
14
15 }
16 //Run the function like so
17 simpleFunction();
18
19 //function with param
20 function sayHello($name = " you out there!"){
21 echo "<br>and<br> Hello $name<br>";
22 }
23 sayHello('John');
24 sayHello();
25
26 //Reurn Value
27 function addNumbers($num1, $num2){
28 return $num1 + $num2;
29 }
30 echo addNumbers(2,3);
31
32 // By Reference
33
34 $myNum = 10;
35
36 function addFive($num){
37 $num += 5;
38 }
39
40 function addTen(&$num) {
41 $num += 10;
42 }
43
44 addFive($myNum);
45 echo "<br>Value: $myNum<br>";
46
47 addTen($myNum);
48 echo "Value: $myNum<br>";
49
50
51?>
1// Inherit $message
2$example = function () use ($message) {
3 var_dump($message);
4};
5$example();