0% found this document useful (0 votes)
39 views10 pages

Ipt2 - Chapter 3

The document discusses PHP functions. It defines what a PHP function is and the advantages of using functions like code reusability and less code. It provides examples of declaring user-defined functions with the proper syntax. It also demonstrates how to pass arguments to functions, return values from functions, and use default argument values. The document contains lessons on parameterized functions and default argument values functions with additional examples.

Uploaded by

ladymx123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

Ipt2 - Chapter 3

The document discusses PHP functions. It defines what a PHP function is and the advantages of using functions like code reusability and less code. It provides examples of declaring user-defined functions with the proper syntax. It also demonstrates how to pass arguments to functions, return values from functions, and use default argument values. The document contains lessons on parameterized functions and default argument values functions with additional examples.

Uploaded by

ladymx123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2

CHAPTER 3: PHP Functions

Objectives:
a. Discuss the advantage of using PHP Functions.
b. Recognize the syntax in declaring a function.
c. Create a program using a function.

Lesson 1: PHP Function Overview


PHP Functions
PHP function is a piece of code that can be reused many times. It can take input as argument
list and return value. There are thousands of built-in functions in PHP.
In PHP, we can define Conditional function, Function within Function and Recursive
function also.

Advantage of PHP Functions


Code Reusability: PHP functions are defined only once and can be invoked many times, like in
other programming languages.
Less Code: It saves a lot of code because you don't need to write the logic many times. By the
use of function, you can write the logic only once and reuse it.
9.9M
238
C++ vs Java
Easy to understand: PHP functions separate the programming logic. So it is easier to
understand the flow of the application because every logic is divided in the form of functions.

PHP User-defined Functions


We can declare and call user-defined functions easily. Let's see the syntax to declare user-
defined functions.
Syntax
Page | 1
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2

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

Lesson 2: PHP Parameterized Function


PHP Parameterized Function
PHP Parameterized functions are the functions with parameters. You can pass any number of
parameters inside a function. These passed parameters act as variables inside your function.
They are specified inside the parentheses, after the function name.
The output depends upon the dynamic values passed as the parameters into the function.

PHP Parameterized Example 1


Addition and Subtraction
7. Make a Router using Active Record Pattern Php | Build a CMS using OOP PHP CMS tutorial
MVC [2020]
In this example, we have passed two parameters $x and $y inside two
functions add() and sub().
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Parameter Addition and Subtraction Example</title>

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

PHP Parameterized Example 2


Addition and Subtraction with Dynamic number
In this example, we have passed two parameters $x and $y inside two
functions add() and sub().
1. <?php
2. //add() function with two parameter
3. function add($x,$y)
4. {
5. $sum=$x+$y;
6. echo "Sum = $sum <br><br>";
7. }
8. //sub() function with two parameter
9. function sub($x,$y)
10. {
11. $sub=$x-$y;
12. echo "Diff = $sub <br><br>";
13. }
14. //call function, get two argument through input box and click on add or sub button
15. if(isset($_POST['add']))
16. {
17. //call add() function
18. add($_POST['first'],$_POST['second']);
19. }
20. if(isset($_POST['sub']))
21. {
22. //call add() function
23. sub($_POST['first'],$_POST['second']);

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:

We passed the following number,

Now clicking on ADDITION button, we get the following output.

Page | 7
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2

Now clicking on SUBTRACTION button, we get the following output.

For more info about PHP Parameterized Function, please click the link below:
https://www.youtube.com/watch?v=I9XkWyets9w

Lesson 3: PHP Default Argument Values Function


PHP Default Argument Values Function
PHP allows you to define C++ style default argument values. In such case, if you don't pass any
value to the function, it will use default argument value.
Let' see the simple example of using PHP default arguments in function.
Example 1
1. <?php
2. function sayHello($name="Ram"){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Sonoo");
6. sayHello();//passing no value
7. sayHello("Vimal");
8. ?>
Output:
Hello Sonoo
Hello Ram

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

You might also like