SlideShare a Scribd company logo
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.
Example: 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 begins 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 to 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:
1. Indexed arrays - Arrays with a numeric index
2. Associative arrays - Arrays with named keys
3. 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.
Syntax 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"=>"20
0000");
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

More Related Content

PPTX
Arrays &amp; functions in php
PPTX
Introduction to PHP_ Lexical structure_Array_Function_String
PPTX
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PDF
Php Tutorials for Beginners
PDF
php AND MYSQL _ppt.pdf
PDF
Hsc IT 5. Server-Side Scripting (PHP).pdf
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
PPTX
function in php like control loop and its uses
Arrays &amp; functions in php
Introduction to PHP_ Lexical structure_Array_Function_String
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
Php Tutorials for Beginners
php AND MYSQL _ppt.pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
Php my sql - functions - arrays - tutorial - programmerblog.net
function in php like control loop and its uses

Similar to PHP FUNCTIONS AND ARRAY.pptx (20)

PDF
PHP-Part3
PPTX
function in php using like three type of function
PPTX
Functions in PHP.pptx
PDF
Web Design EJ3
PPT
Php Chapter 2 3 Training
PPT
PHP-03-Functions.ppt
PPT
PHP-03-Functions.ppt
PDF
PHP Lec 2.pdfsssssssssssssssssssssssssss
PPTX
Introduction To PHP000000000000000000000000000000.pptx
PPSX
DIWE - Advanced PHP Concepts
DOCX
Array andfunction
PPTX
Web Application Development using PHP Chapter 3
PDF
PHP Unit 3 functions_in_php_2
PPTX
PPT
Web Technology_10.ppt
PPT
PHP complete reference with database concepts for beginners
PPTX
php basics
PPT
PHP Scripting
PDF
Array String - Web Programming
PHP-Part3
function in php using like three type of function
Functions in PHP.pptx
Web Design EJ3
Php Chapter 2 3 Training
PHP-03-Functions.ppt
PHP-03-Functions.ppt
PHP Lec 2.pdfsssssssssssssssssssssssssss
Introduction To PHP000000000000000000000000000000.pptx
DIWE - Advanced PHP Concepts
Array andfunction
Web Application Development using PHP Chapter 3
PHP Unit 3 functions_in_php_2
Web Technology_10.ppt
PHP complete reference with database concepts for beginners
php basics
PHP Scripting
Array String - Web Programming
Ad

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Structure & Organelles in detailed.
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Lesson notes of climatology university.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Complications of Minimal Access Surgery at WLH
Cell Structure & Organelles in detailed.
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Chinmaya Tiranga quiz Grand Finale.pdf
GDM (1) (1).pptx small presentation for students
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
Presentation on HIE in infants and its manifestations
Pharmacology of Heart Failure /Pharmacotherapy of CHF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial diseases, their pathogenesis and prophylaxis
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Lesson notes of climatology university.
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Ad

PHP FUNCTIONS AND ARRAY.pptx

  • 1. PHP FUNCTIONS & ARRAYS Dr.R.Shalini M.Sc.,MPhil.,PhD., Assistant Professor VISTAS
  • 2. 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.
  • 3. Two major types of functions: 1.Built-in functions: These functions are already coded and stored in form of functions. Example: 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 begins 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.
  • 4. • 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. PHP Functions - Returning Value Functions can also return values to 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); ?>
  • 9. 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(&).
  • 10. <?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
  • 11. 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
  • 12. In PHP, there are three types of arrays: 1. Indexed arrays - Arrays with a numeric index 2. Associative arrays - Arrays with named keys 3. Multidimensional arrays - Arrays containing one or more arrays
  • 13. 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";
  • 14. 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
  • 15. 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
  • 16. 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. Syntax for associative arrays: array(key=>value,key=>value,key=>value,etc.) Parameter Description key Specifies the key (numeric or string) value Specifies the value
  • 17. 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"=>"20 0000"); 2nd way: $salary["Sonoo"]="350000"; $salary["John"]="450000"; $salary["Kartik"]="200000";
  • 18. 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
  • 19. 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
  • 20. 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) );
  • 21. 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
  • 22. 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