SlideShare a Scribd company logo
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
1
Chapter 5
Part III
PHP
1. PHP User Defined Functions
 Besides the built-in PHP functions, we can create our own functions.
 A function is a block of statements that can be used repeatedly in a program.
 A function will not execute immediately when a page loads.
 A function will be executed by a call to the function.
A user defined function declaration starts with the word "function":
Syntax
function functionName() {
code to be executed;
}
In the example below, 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:
Example
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
2
PHP Function Arguments
Information can be passed to functions through arguments. An argument is just like
a variable.
Arguments are specified after the function name, inside the parentheses. You can
add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When the
familyName() function is called, we also pass along a name (e.g. Jani), and the
name is used inside the function, which outputs several different first names, but an
equal last name:
Example:
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
The following example has a function with two arguments ($fname and $year):
Example
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
3
}
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
PHP Default Argument Value
The following example shows how to use a default parameter. If we call the
function setHeight() without arguments it takes the default value as argument:
Example
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
2. PHP Arrays
An array stores multiple values in one single variable:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
4
What is an Array?
An array is a special variable, which can hold more than one value at a
time.
If you have a list of items (a list of car names, for example), storing the
cars in single variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars and find a specific
one? And what if you had not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access
the values by referring to an index number.
Create an Array in PHP
In PHP, the array() function is used to create an array:
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
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
5
PHP Indexed Arrays
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like
this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars, assigns
three elements to it, and then prints a text containing the array values:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Get The Length of an Array - The count() Function
The count() function is used to return the length (the number of
elements) of an array:
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
6
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Loop Through an Indexed Array
To loop through and print all the values of an indexed array, you could
use a for loop, like this:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to
them.
There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood
7
Or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
The named keys can then be used in a script:
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Loop Through an Associative Array
To loop through and print all the values of an associative array, you
could use a foreach loop, like this:
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>

More Related Content

What's hot

Php, mysq lpart1
Php, mysq lpart1Php, mysq lpart1
Php, mysq lpart1
Subhasis Nayak
 
Basics PHP
Basics PHPBasics PHP
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
Yoeung Vibol
 
Php Loop
Php LoopPhp Loop
Php Looplotlot
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
Santhiya Grace
 
PHP-Part1
PHP-Part1PHP-Part1
PHP-Part1
Ahmed Saihood
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009
cwarren
 
Php variables (english)
Php variables (english)Php variables (english)
Php variables (english)
Mahmoud Masih Tehrani
 
Php string function
Php string function Php string function
Php string function
Ravi Bhadauria
 
php basics
php basicsphp basics
php basics
Anmol Paul
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
Ashish Chamoli
 
PHP Basic & Variables
PHP Basic & VariablesPHP Basic & Variables
PHP Basic & Variables
M.Zalmai Rahmani
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
Elmer Concepcion Jr.
 

What's hot (20)

Php, mysq lpart1
Php, mysq lpart1Php, mysq lpart1
Php, mysq lpart1
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Php Loop
Php LoopPhp Loop
Php Loop
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
PHP-Part1
PHP-Part1PHP-Part1
PHP-Part1
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009
 
Php variables (english)
Php variables (english)Php variables (english)
Php variables (english)
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Php string function
Php string function Php string function
Php string function
 
Php basics
Php basicsPhp basics
Php basics
 
php basics
php basicsphp basics
php basics
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
 
PHP Basic & Variables
PHP Basic & VariablesPHP Basic & Variables
PHP Basic & Variables
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 

Viewers also liked

Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02
Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02
Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02
SERGIO DE MELLO QUEIROZ
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
Sayed Ahmed
 
Immigration Counts
Immigration CountsImmigration Counts
Immigration Counts
Penn State University
 
php 1
php 1php 1
php 1
tumetr1
 
La Fotografia al crocevia delle Arti
La Fotografia al crocevia delle ArtiLa Fotografia al crocevia delle Arti
La Fotografia al crocevia delle Arti
Cristiano Maria Gaston
 
Plan de cultura ciudadana u
Plan de cultura ciudadana uPlan de cultura ciudadana u
Plan de cultura ciudadana u
katina toro
 
Aprendizaje ubicuo
Aprendizaje ubicuoAprendizaje ubicuo
Aprendizaje ubicuo
alejandra parville
 
αφηγηματολογια
αφηγηματολογιααφηγηματολογια
αφηγηματολογια
MANOLIS MORAITIS
 
Cc unidad 2 act 1(maribel garcía)
Cc unidad 2 act 1(maribel garcía)Cc unidad 2 act 1(maribel garcía)
Cc unidad 2 act 1(maribel garcía)
marilgarcia7
 
Victor Adimike Resume
Victor Adimike ResumeVictor Adimike Resume
Victor Adimike Resume
Victor Adimike
 
WordPress SEO Success
WordPress SEO SuccessWordPress SEO Success
WordPress SEO Success
Jake Aull
 
Power point tic
Power point ticPower point tic
Power point tic
yoanaorrego
 
Maduracion de las_celulas_sexuales
Maduracion de las_celulas_sexualesMaduracion de las_celulas_sexuales
Maduracion de las_celulas_sexuales
Maiten Machuca
 
الفصل الثالث تنظيم السلطات في دولة الإمارات
الفصل الثالث تنظيم السلطات في دولة الإماراتالفصل الثالث تنظيم السلطات في دولة الإمارات
الفصل الثالث تنظيم السلطات في دولة الإمارات
حمود الشحي
 
Schedule table - Deyhook 2-92-10
Schedule table - Deyhook 2-92-10Schedule table - Deyhook 2-92-10
Schedule table - Deyhook 2-92-10Reza Taheriattar
 

Viewers also liked (15)

Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02
Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02
Manualdopropietariocb450dxtodasd1201 man-0004-140817100956-phpapp02
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Immigration Counts
Immigration CountsImmigration Counts
Immigration Counts
 
php 1
php 1php 1
php 1
 
La Fotografia al crocevia delle Arti
La Fotografia al crocevia delle ArtiLa Fotografia al crocevia delle Arti
La Fotografia al crocevia delle Arti
 
Plan de cultura ciudadana u
Plan de cultura ciudadana uPlan de cultura ciudadana u
Plan de cultura ciudadana u
 
Aprendizaje ubicuo
Aprendizaje ubicuoAprendizaje ubicuo
Aprendizaje ubicuo
 
αφηγηματολογια
αφηγηματολογιααφηγηματολογια
αφηγηματολογια
 
Cc unidad 2 act 1(maribel garcía)
Cc unidad 2 act 1(maribel garcía)Cc unidad 2 act 1(maribel garcía)
Cc unidad 2 act 1(maribel garcía)
 
Victor Adimike Resume
Victor Adimike ResumeVictor Adimike Resume
Victor Adimike Resume
 
WordPress SEO Success
WordPress SEO SuccessWordPress SEO Success
WordPress SEO Success
 
Power point tic
Power point ticPower point tic
Power point tic
 
Maduracion de las_celulas_sexuales
Maduracion de las_celulas_sexualesMaduracion de las_celulas_sexuales
Maduracion de las_celulas_sexuales
 
الفصل الثالث تنظيم السلطات في دولة الإمارات
الفصل الثالث تنظيم السلطات في دولة الإماراتالفصل الثالث تنظيم السلطات في دولة الإمارات
الفصل الثالث تنظيم السلطات في دولة الإمارات
 
Schedule table - Deyhook 2-92-10
Schedule table - Deyhook 2-92-10Schedule table - Deyhook 2-92-10
Schedule table - Deyhook 2-92-10
 

Similar to PHP-Part3

Introduction to php
Introduction to phpIntroduction to php
Introduction to php
Taha Malampatti
 
PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array.
wahidullah mudaser
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
Prompt engineering for iOS developers (How LLMs and GenAI work)
Prompt engineering for iOS developers (How LLMs and GenAI work)Prompt engineering for iOS developers (How LLMs and GenAI work)
Prompt engineering for iOS developers (How LLMs and GenAI work)
Andrey Volobuev
 
Loops in C# for loops while and do while loop.
Loops in C# for loops while and do while loop.Loops in C# for loops while and do while loop.
Loops in C# for loops while and do while loop.
Abid Kohistani
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomer
Shivi Tomer
 
Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]srikanthbkm
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
Web programming
Web programmingWeb programming
Web programming
Leo Mark Villar
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
team11vgnt
 
Php
PhpPhp
Java script basics
Java script basicsJava script basics
Java script basics
John Smith
 
Javascript
JavascriptJavascript
Javascript
orestJump
 
Php
PhpPhp
Php interview questions
Php interview questionsPhp interview questions
Php interview questions
sekar c
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
Arti Parab Academics
 

Similar to PHP-Part3 (20)

Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array.
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Prompt engineering for iOS developers (How LLMs and GenAI work)
Prompt engineering for iOS developers (How LLMs and GenAI work)Prompt engineering for iOS developers (How LLMs and GenAI work)
Prompt engineering for iOS developers (How LLMs and GenAI work)
 
Loops in C# for loops while and do while loop.
Loops in C# for loops while and do while loop.Loops in C# for loops while and do while loop.
Loops in C# for loops while and do while loop.
 
Php Basics
Php BasicsPhp Basics
Php Basics
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomer
 
Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]
 
Java Script
Java ScriptJava Script
Java Script
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
Web programming
Web programmingWeb programming
Web programming
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Php
PhpPhp
Php
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Javascript
JavascriptJavascript
Javascript
 
Php
PhpPhp
Php
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questions
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 

More from Ahmed Saihood

Sessions &Cookies
Sessions &CookiesSessions &Cookies
Sessions &Cookies
Ahmed Saihood
 
HTTP & HTTPs
HTTP & HTTPsHTTP & HTTPs
HTTP & HTTPs
Ahmed Saihood
 
CSS
CSSCSS
XHTML
XHTMLXHTML
HTML-Forms
HTML-FormsHTML-Forms
HTML-Forms
Ahmed Saihood
 
HTML-Part2
HTML-Part2HTML-Part2
HTML-Part2
Ahmed Saihood
 
HTML-Part1
HTML-Part1HTML-Part1
HTML-Part1
Ahmed Saihood
 
internet basics
internet basics internet basics
internet basics
Ahmed Saihood
 

More from Ahmed Saihood (8)

Sessions &Cookies
Sessions &CookiesSessions &Cookies
Sessions &Cookies
 
HTTP & HTTPs
HTTP & HTTPsHTTP & HTTPs
HTTP & HTTPs
 
CSS
CSSCSS
CSS
 
XHTML
XHTMLXHTML
XHTML
 
HTML-Forms
HTML-FormsHTML-Forms
HTML-Forms
 
HTML-Part2
HTML-Part2HTML-Part2
HTML-Part2
 
HTML-Part1
HTML-Part1HTML-Part1
HTML-Part1
 
internet basics
internet basics internet basics
internet basics
 

Recently uploaded

History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptxLiving-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
TristanJasperRamos
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
Output determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CCOutput determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CC
ShahulHameed54211
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
Himani415946
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 

Recently uploaded (16)

History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptxLiving-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
Output determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CCOutput determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CC
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 

PHP-Part3

  • 1. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 1 Chapter 5 Part III PHP 1. PHP User Defined Functions  Besides the built-in PHP functions, we can create our own functions.  A function is a block of statements that can be used repeatedly in a program.  A function will not execute immediately when a page loads.  A function will be executed by a call to the function. A user defined function declaration starts with the word "function": Syntax function functionName() { code to be executed; } In the example below, 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: Example <?php function writeMsg() { echo "Hello world!"; } writeMsg(); // call the function ?>
  • 2. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 2 PHP Function Arguments Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name: Example: <?php function familyName($fname) { echo "$fname Refsnes.<br>"; } familyName("Jani"); familyName("Hege"); familyName("Stale"); familyName("Kai Jim"); familyName("Borge"); ?> The following example has a function with two arguments ($fname and $year): Example <?php function familyName($fname, $year) { echo "$fname Refsnes. Born in $year <br>";
  • 3. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 3 } familyName("Hege", "1975"); familyName("Stale", "1978"); familyName("Kai Jim", "1983"); ?> PHP Default Argument Value The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument: Example <?php function setHeight($minheight = 50) { echo "The height is : $minheight <br>"; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80); ?> 2. PHP Arrays An array stores multiple values in one single variable: Example <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
  • 4. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 4 What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: $cars1 = "Volvo"; $cars2 = "BMW"; $cars3 = "Toyota"; However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? The solution is to create an array! An array can hold many values under a single name, and you can access the values by referring to an index number. Create an Array in PHP In PHP, the array() function is used to create an array: 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
  • 5. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 5 PHP Indexed Arrays There are two ways to create indexed arrays: The index can be assigned automatically (index always starts at 0), like this: $cars = array("Volvo", "BMW", "Toyota"); or the index can be assigned manually: $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota"; The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values: Example <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?> Get The Length of an Array - The count() Function The count() function is used to return the length (the number of elements) of an array:
  • 6. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 6 Example <?php $cars = array("Volvo", "BMW", "Toyota"); echo count($cars); ?> Loop Through an Indexed Array To loop through and print all the values of an indexed array, you could use a for loop, like this: Example <?php $cars = array("Volvo", "BMW", "Toyota"); $arrlength = count($cars); for($x = 0; $x < $arrlength; $x++) { echo $cars[$x]; echo "<br>"; } ?> PHP Associative Arrays Associative arrays are arrays that use named keys that you assign to them. There are two ways to create an associative array: $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
  • 7. Subject: Web Design and Programming Lecturer: Ahmed Ali Saihood 7 Or: $age['Peter'] = "35"; $age['Ben'] = "37"; $age['Joe'] = "43"; The named keys can then be used in a script: Example <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?> Loop Through an Associative Array To loop through and print all the values of an associative array, you could use a foreach loop, like this: Example <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?>