PHP FUNCTIONS AND ARRAY.pptx

ShaliniPrabakaran 31 views 23 slides Sep 05, 2023
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

PHP FUNCTIONS AND ARRAYS


Slide Content

PHP FUNCTIONS & ARRAYS Dr.R.Shalini M.Sc.,MPhil.,PhD ., Assistant Professor VISTAS

PHP FUNCTIONS Definition : 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. 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: By the use of function, one can write the logic only once and reuse it. 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.

Two major types of functions : 1.Built-in functions: These functions are already coded and stored in form of functions. Exampl e: var_dump , fopen (), print_r (), gettype () and so on. 2.User Defined Functions : PHP allows to create own customized functions called the user-defined functions. While creating a user defined function few things to be considered are Any name ending with an open and closed parenthesis is a function . A function name always begin s with the keyword function . To call a function we just need to write its name followed by the parenthesis A function name cannot start with a number. It can start with an alphabet or underscore. A function name is not case-sensitive .

A user-defined function declaration starts with the word function: Syntax function functionName () { code to be executed; } Example: 1 <? php function writeMsg () { echo "Hello world!"; } writeMsg (); // call the function ?> Output : Hello world

Explanation for the above program : we create a function named " writeMsg ()". The opening curly brace ( { ) indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of the function. The function outputs "Hello world!". To call the function, just write its name followed by brackets (): Example: 2 <? php function addFunction ($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } addFunction (10, 20); ?> Output: Sum of the two numbers is : 30

PHP Function Arguments Information can be passed to functions through arguments which is separated by comma. It is just like a variable and are specified after the function name, inside the parentheses. One can add as many arguments as wanted. Example: 1 <? php function sayHello ($name) { echo "Hello $name< br />"; Output: } sayHello (" Sonoo "); sayHello ("Vimal"); sayHello ("John"); ?> Hello Sonoo Hello Vimal Hello John

Example 1 Program Explanation The following example has a function with one argument ($name). When the sayhello () function is called, we also pass along a name (e.g. sonoo ), And the name is used inside the function, which outputs several different names. Example: 2 Function with two arguments <? php function sayHello ($ name,$age ) { echo "Hello $name, you are $age years old< br />"; } sayHello ("Sonoo",27); output: sayHello ("Vimal",29); Hello Sonoo , you are 27 years old sayHello ("John",23); ?> Hello Vimal, you are 29 years old Hello John, you are 23 years old

PHP Functions - Returning Value Functions can also return values t o the part of program from where it is called. The return keyword is used to return value back to the part of program, from where it was called. The returning value may be of any type including the arrays and objects. The return statement also marks the end of the function and stops the execution after that and returns the value. Example: <? php function circle($r) { output return 3.14*$r*$r; } echo "Area of circle is: ".circle(3); ?>

Parameter passing to Functions PHP allows us two ways in which an argument can be passed into a function: Pass by Value: On passing arguments using pass by value, the value of the argument gets changed within a function, but the original value outside the function remains unchanged. That means a duplicate of the original value is passed as an argument. Pass by Reference: On passing arguments as pass by reference, the original value is passed. Therefore, the original value gets altered. In pass by reference we actually pass the address of the value, where it is stored using ampersand sign(&).

<? php // pass by value function valfun ($ num ) { $ num += 2; return $ num ; } // pass by reference function reffun (&$ num ) { $ num += 10; return $ num ; } $n = 10; val ($n); echo "The original value is still $n \n"; ref($n); echo "The original value changes to $n"; ?>  Output:    The original value is still 10 The original value changes to 20

Definition: An array is a data structure that stores one or more similar type of values in a single value. An array is created using an array( ) function . //create an empty array: $ arr = array(); //create an array with elements 10, 20, 30: $ arr = array(10, 20, 30); //create an array with mixed types: $ arr = array(10, 3.14, "Hello"); //get the first element: $x = $ arr [0]; //x has value 10 PHP - Arrays

In PHP, there are three types of arrays : Indexed arrays - Arrays with a numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays

Numeric array PHP index is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default. There are two ways to define indexed array : 1st way: $season=array(" summer","winter","spring","autumn "); 2nd way: $season[0]="summer"; $season[1]="winter"; $season[2]="spring"; $season[3]="autumn";

Example1 <? php $season=array(" summer","winter","spring","autumn "); echo "Season are: $season[0], $season[1], $season[2] and $season[3]"; ?> Output: Season are: summer, winter, spring and autumn

Example:2 <? php $season[0]="summer"; $season[1]="winter"; $season[2]="spring"; $season[3]="autumn"; echo "Season are: $season[0], $season[1], $season[2] and $season[3]"; ?> Output: Season are: summer, winter, spring and autumn

Associative array An array with strings as index . This stores element values in association with key values rather than in a strict linear index order. S yntax for associative arrays : array(key=> value,key => value,key => value,etc .) Parameter Description key Specifies the key (numeric or string) value Specifies the value

We can associate name with each array elements in PHP using => symbol. There are two ways to define associative array: 1st way: $salary=array(" Sonoo "=>"350000","John"=>"450000","Kartik"=>"200000"); 2nd way: $salary[" Sonoo "]="350000"; $salary["John"]="450000"; $salary["Kartik"]="200000";

Example 1 <? php $salary=array(" Sonoo "=>"350000","John"=>"450000","Kartik"=>"200000"); echo " Sonoo salary: ".$salary[" Sonoo "]."< br />"; echo "John salary: ".$salary["John"]."< br />"; echo "Kartik salary: ".$salary["Kartik"]."< br />"; ?> Output: Sonoo salary: 350000 John salary: 450000 Kartik salary: 200000

Example:2 <? php $salary[" Sonoo "]="350000"; $salary["John"]="450000"; $salary["Kartik"]="200000"; echo " Sonoo salary: ".$salary[" Sonoo "]."< br />"; echo "John salary: ".$salary["John"]."< br />"; echo "Kartik salary: ".$salary["Kartik"]."< br />"; ?> Output: Sonoo salary: 350000 John salary: 450000 Kartik salary: 200000

PHP Multidimensional Array PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column . Definition: $emp = array ( array(1,"sonoo",400000), array(2,"john",500000), array(3,"rahul",300000) );

Example of PHP multidimensional array to display following tabular data as 3 rows and 3 columns. Id Name Salary 1 sonoo 400000 2 john 500000 3 rahul 300000

Example: <? php $emp = array ( array(1,"sonoo",400000), array(2,"john",500000), array(3,"rahul",300000) ); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col]." "; } echo "< br />"; } ?> Output : 1 sonoo 400000 2 john 500000 3 rahul 300000

THANK YOU