Ipt2 - Chapter 3
Ipt2 - Chapter 3
Objectives:
a. Discuss the advantage of using PHP Functions.
b. Recognize the syntax in declaring a function.
c. Create a program using a function.
1. function functionname(){
2. //code to be executed
3. }
PHP Functions Example
File: function1.php
1. <?php
2. function sayHello(){
3. echo "Hello PHP Function";
4. }
5. sayHello();//calling function
6. ?>
PHP Function Arguments
We can pass the information in PHP function through arguments which is separated by comma.
PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-
length argument list.
Let's see the example to pass single argument in PHP function.
File: functionarg.php
1. <?php
2. function sayHello($name){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Sonoo");
6. sayHello("Vimal");
7. sayHello("John");
8. ?>
PHP Call By Reference
Value passed to the function doesn't modify the actual value by default (call by value). But we
can do so by passing value as a reference.
Page | 2
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
By default, value passed to the function is call by value. To pass value as a reference, you need
to use ampersand (&) symbol before the argument name.
Let's see a simple example of call by reference in PHP.
File: functionref.php
1. <?php
2. function adder(&$str2)
3. {
4. $str2 .= 'Call By Reference';
5. }
6. $str = 'Hello ';
7. adder($str);
8. echo $str;
9. ?>
PHP Function: Default Argument Value
We can specify a default argument value in function. While calling PHP function if you don't
specify any argument, it will take the default argument. Let's see a simple example of using
default argument value in PHP function.
File: functiondefaultarg.php
1. <?php
2. function sayHello($name="Sonoo"){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Rajesh");
6. sayHello();//passing no value
7. sayHello("John");
8. ?>
PHP Function: Returning Value
Let's see an example of PHP function that returns value.
Page | 3
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
File: functiondefaultarg.php
1. <?php
2. function cube($n){
3. return $n*$n*$n;
4. }
5. echo "Cube of 3 is: ".cube(3);
6. ?>
For more info about PHP Function, please click the link below:
https://www.youtube.com/watch?v=RIPJEgOrVRc
Page | 4
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
5. </head>
6. <body>
7. <?php
8. //Adding two numbers
9. function add($x, $y) {
10. $sum = $x + $y;
11. echo "Sum of two numbers is = $sum <br><br>";
12. }
13. add(467, 943);
14.
15. //Subtracting two numbers
16. function sub($x, $y) {
17. $diff = $x - $y;
18. echo "Difference between two numbers is = $diff";
19. }
20. sub(943, 467);
21. ?>
22. </body>
23. </html>
Output:
Page | 5
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
Page | 6
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
24. }
25. ?>
26. <form method="post">
27. Enter first number: <input type="number" name="first"/><br><br>
28. Enter second number: <input type="number" name="second"/><br><br>
29. <input type="submit" name="add" value="ADDITION"/>
30. <input type="submit" name="sub" value="SUBTRACTION"/>
31. </form>
Output:
Page | 7
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
For more info about PHP Parameterized Function, please click the link below:
https://www.youtube.com/watch?v=I9XkWyets9w
Page | 8
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
Hello Vimal
Since PHP 5, you can use the concept of default argument value with call by reference also.
Example 2
1. <?php
2. function greeting($first="Sonoo",$last="Jaiswal"){
3. echo "Greeting: $first $last<br/>";
4. }
5. greeting();
6. greeting("Rahul");
7. greeting("Michael","Clark");
8. ?>
Output:
HTML Tutorial
Greeting: Sonoo Jaiswal
Greeting: Rahul Jaiswal
Greeting: Michael Clark
Example 3
1. <?php
2. function add($n1=10,$n2=10){
3. $n3=$n1+$n2;
4. echo "Addition is: $n3<br/>";
5. }
6. add();
7. add(20);
8. add(40,40);
9. ?>
Page | 9
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
Output:
Addition is: 20
Addition is: 30
Addition is: 80
For more info about PHP Default Argument Values Function , please click the link
below: https://www.youtube.com/watch?v=Sf8aZh1zYyA
REFERENCES
Page | 10