SlideShare a Scribd company logo
3
Most read
4
Most read
15
Most read
PHP Function
By:- Jalpesh Vasa
PHP Functions
• Heart of well-organized script
• Block of code that is not immediately
executed, but can be called by your script
when needed.
• Basically 2 types:
 Built-in
 User-defined
PHP Functions(Built in)
• PHP Array Function
• PHP Calendar Function
• PHP class/object Function
• PHP Date/Time Function
• PHP Directory Function
• PHP Error Handling Function
• PHP File System Function
• PHP MySQL Function
• PHP Network Function
• PHP String Function
• PHP ODBC Function
• PHP XML Parsing Function
PHP Functions (Built-in)
• include() – include content of one file to php
file.
 gives warning if file not found and
continuing executing script
• require() - compulsory include the specified
file.
 gives fatal error and stops executing script
furhter .
PHP Functions (Built-in)
• Eg. <html><body><h1>hello</h1><p> ok</p>
<?php include(‘a1.php’);?>
<?php include ‘a1.txt’; ?>
</body></html>
• Eg. <body>
<h1>Welcome to my home page!</h1>
<?php include 'vars.php';
echo "I have a $color $car.";
?>
</body>
<?php
$color='red';
$car='BMW';
?>
vars.php
PHP Functions (Built-in)
• Eg. <html><body><h1>hello</h1><p> ok</p>
<?php include ‘vars1.php’;
echo “you have $color $car”;
?>
</body></html>
• Eg. <body>
<h1>Welcome to my home page!</h1>
<?php require 'vars1.php';
echo "I have a $color $car.";
?>
</body>
<?php
$color='red';
$car='BMW';
?>
vars.php
PHP Functions(User defined)
• Information to be passed to them & usually
return value.
• Function name are case-insensitive
• All function require parenthesis
• Defining a user-defined function:
function fun_name(arg1,arg2)
{
----
----
}
PHP Functions
<?php
function bigh()
{
echo “hello”;
}
bigh();
Bigh();
?>
PHP Functions
<?php
function printB($txt)
{
echo “$txt<br>”;
}
printB(“This is line”);
printB(“hello”);
printB(“hi”);
?>
PHP Functions
<?php
function addnum($n1, $n2)
{
$result = $n1+$n2;
return $result;
}
echo addnum(3,4);
?>
PHP Functions
• Dynamic function call
It is possible to assign function name as strings to
variables & then treat variables exactly function name
itself.
<?php
function say()
{
echo “hello<br>”;
}
$fun_name = “say”;
$fun_name();
?>
PHP Functions
• Variable declared within a function remains
local to that function
 not available outside function or within other
function
function hello()
{ $var1 = “hi”;
echo “variable is $var1”;
}
PHP Functions
$var2 = “hello”;
function f1()
{
echo “variable is $var2”; //inaccessible
}
f1();
PHP Functions
$var2 = 42;
function f1()
{ global $var2;
echo “variable is $var2”; //accessible
}
f1();
PHP Functions
function foo()
{
function bar()
{
echo “hi”; // doesn’t exist until foo is called.
}
}
PHP Functions
function be($h=30)
{
echo $h;
}
Be(450);
be();
be(21);

More Related Content

What's hot (20)

PPTX
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
PDF
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
PPTX
Form Handling using PHP
Nisa Soomro
 
PPT
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
PPTX
Files in php
sana mateen
 
PPT
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
PPTX
Php string function
Ravi Bhadauria
 
PPTX
Php basics
Jamshid Hashimi
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PPT
Php forms
Anne Lee
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPT
01 Php Introduction
Geshan Manandhar
 
PPT
Java Script ppt
Priya Goyal
 
PPTX
Php.ppt
Nidhi mishra
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPTX
Data types in php
ilakkiya
 
PPT
PHP variables
Siddique Ibrahim
 
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
PDF
Php introduction
krishnapriya Tadepalli
 
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
Form Handling using PHP
Nisa Soomro
 
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Files in php
sana mateen
 
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Php string function
Ravi Bhadauria
 
Php basics
Jamshid Hashimi
 
Php forms
Anne Lee
 
Introduction to Javascript
Amit Tyagi
 
01 Php Introduction
Geshan Manandhar
 
Java Script ppt
Priya Goyal
 
Php.ppt
Nidhi mishra
 
PHP Functions & Arrays
Henry Osborne
 
Data types in php
ilakkiya
 
PHP variables
Siddique Ibrahim
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Php introduction
krishnapriya Tadepalli
 

Similar to 4.2 PHP Function (20)

PPTX
Arrays &amp; functions in php
Ashish Chamoli
 
PPTX
Php
Yoga Raja
 
PPTX
Functions in PHP.pptx
Japneet9
 
PDF
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
PPTX
php Chapter 1.pptx
HambaAbebe2
 
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
PDF
Chap 4 PHP.pdf
HASENSEID
 
PDF
Exploring PHP's Built-in Functions
Emma Thompson
 
PDF
Php Tutorials for Beginners
Vineet Kumar Saini
 
PDF
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
PDF
Wt unit 4 server side technology-2
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PDF
PHP in Web development and Applications.pdf
VinayVitekari
 
PPTX
Lecture 9 - Intruduction to BOOTSTRAP.pptx
AOmaAli
 
PDF
Module-3 15CS71-WTA-Serverside Development with PHP
SIVAKUMAR V
 
PPTX
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
PPTX
Introduction in php part 2
Bozhidar Boshnakov
 
PPTX
Lecture3 php by okello erick
okelloerick
 
PDF
WT_PHP_PART1.pdf
HambardeAtharva
 
PPTX
function in php like control loop and its uses
vishal choudhary
 
Arrays &amp; functions in php
Ashish Chamoli
 
Functions in PHP.pptx
Japneet9
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
php Chapter 1.pptx
HambaAbebe2
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
Chap 4 PHP.pdf
HASENSEID
 
Exploring PHP's Built-in Functions
Emma Thompson
 
Php Tutorials for Beginners
Vineet Kumar Saini
 
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
Wt unit 4 server side technology-2
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PHP in Web development and Applications.pdf
VinayVitekari
 
Lecture 9 - Intruduction to BOOTSTRAP.pptx
AOmaAli
 
Module-3 15CS71-WTA-Serverside Development with PHP
SIVAKUMAR V
 
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
Introduction in php part 2
Bozhidar Boshnakov
 
Lecture3 php by okello erick
okelloerick
 
WT_PHP_PART1.pdf
HambardeAtharva
 
function in php like control loop and its uses
vishal choudhary
 
Ad

More from Jalpesh Vasa (16)

PDF
Object Oriented PHP - PART-1
Jalpesh Vasa
 
PDF
Object Oriented PHP - PART-2
Jalpesh Vasa
 
PDF
5. HTML5
Jalpesh Vasa
 
PDF
4.4 PHP Session
Jalpesh Vasa
 
PDF
4.3 MySQL + PHP
Jalpesh Vasa
 
PDF
4.1 PHP Arrays
Jalpesh Vasa
 
PDF
4 Basic PHP
Jalpesh Vasa
 
PDF
3.2.1 javascript regex example
Jalpesh Vasa
 
PDF
3.2 javascript regex
Jalpesh Vasa
 
PDF
3. Java Script
Jalpesh Vasa
 
PDF
3.1 javascript objects_DOM
Jalpesh Vasa
 
PDF
2 introduction css
Jalpesh Vasa
 
PDF
1 web technologies
Jalpesh Vasa
 
PDF
Remote Method Invocation in JAVA
Jalpesh Vasa
 
PDF
Kotlin for android development
Jalpesh Vasa
 
PDF
Security in php
Jalpesh Vasa
 
Object Oriented PHP - PART-1
Jalpesh Vasa
 
Object Oriented PHP - PART-2
Jalpesh Vasa
 
5. HTML5
Jalpesh Vasa
 
4.4 PHP Session
Jalpesh Vasa
 
4.3 MySQL + PHP
Jalpesh Vasa
 
4.1 PHP Arrays
Jalpesh Vasa
 
4 Basic PHP
Jalpesh Vasa
 
3.2.1 javascript regex example
Jalpesh Vasa
 
3.2 javascript regex
Jalpesh Vasa
 
3. Java Script
Jalpesh Vasa
 
3.1 javascript objects_DOM
Jalpesh Vasa
 
2 introduction css
Jalpesh Vasa
 
1 web technologies
Jalpesh Vasa
 
Remote Method Invocation in JAVA
Jalpesh Vasa
 
Kotlin for android development
Jalpesh Vasa
 
Security in php
Jalpesh Vasa
 
Ad

Recently uploaded (20)

PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 

4.2 PHP Function

  • 2. PHP Functions • Heart of well-organized script • Block of code that is not immediately executed, but can be called by your script when needed. • Basically 2 types:  Built-in  User-defined
  • 3. PHP Functions(Built in) • PHP Array Function • PHP Calendar Function • PHP class/object Function • PHP Date/Time Function • PHP Directory Function • PHP Error Handling Function • PHP File System Function • PHP MySQL Function • PHP Network Function • PHP String Function • PHP ODBC Function • PHP XML Parsing Function
  • 4. PHP Functions (Built-in) • include() – include content of one file to php file.  gives warning if file not found and continuing executing script • require() - compulsory include the specified file.  gives fatal error and stops executing script furhter .
  • 5. PHP Functions (Built-in) • Eg. <html><body><h1>hello</h1><p> ok</p> <?php include(‘a1.php’);?> <?php include ‘a1.txt’; ?> </body></html> • Eg. <body> <h1>Welcome to my home page!</h1> <?php include 'vars.php'; echo "I have a $color $car."; ?> </body> <?php $color='red'; $car='BMW'; ?> vars.php
  • 6. PHP Functions (Built-in) • Eg. <html><body><h1>hello</h1><p> ok</p> <?php include ‘vars1.php’; echo “you have $color $car”; ?> </body></html> • Eg. <body> <h1>Welcome to my home page!</h1> <?php require 'vars1.php'; echo "I have a $color $car."; ?> </body> <?php $color='red'; $car='BMW'; ?> vars.php
  • 7. PHP Functions(User defined) • Information to be passed to them & usually return value. • Function name are case-insensitive • All function require parenthesis • Defining a user-defined function: function fun_name(arg1,arg2) { ---- ---- }
  • 8. PHP Functions <?php function bigh() { echo “hello”; } bigh(); Bigh(); ?>
  • 9. PHP Functions <?php function printB($txt) { echo “$txt<br>”; } printB(“This is line”); printB(“hello”); printB(“hi”); ?>
  • 10. PHP Functions <?php function addnum($n1, $n2) { $result = $n1+$n2; return $result; } echo addnum(3,4); ?>
  • 11. PHP Functions • Dynamic function call It is possible to assign function name as strings to variables & then treat variables exactly function name itself. <?php function say() { echo “hello<br>”; } $fun_name = “say”; $fun_name(); ?>
  • 12. PHP Functions • Variable declared within a function remains local to that function  not available outside function or within other function function hello() { $var1 = “hi”; echo “variable is $var1”; }
  • 13. PHP Functions $var2 = “hello”; function f1() { echo “variable is $var2”; //inaccessible } f1();
  • 14. PHP Functions $var2 = 42; function f1() { global $var2; echo “variable is $var2”; //accessible } f1();
  • 15. PHP Functions function foo() { function bar() { echo “hi”; // doesn’t exist until foo is called. } }
  • 16. PHP Functions function be($h=30) { echo $h; } Be(450); be(); be(21);