SlideShare a Scribd company logo
Arrays & functions in php
Functions In PHP
 Functions are at the heart of a well-organized script and
will make your code easy to read and reuse.
 Large projects would be unmanageable without functions
because the problem of repetitive code would bog down
the development process.
 If you haven’t had much experience using functions, you
can think of a function as an input/output machine. The
machine takes the raw materials you feed it (the input)
and works with them to produce a product (the output).
 A function accepts values, processes them, and then
performs an action (printing to the browser, for example),
returns a new value, or both.
Functions In PHP
 If you need to bake a cake, you would probably do it
yourself, in your own kitchen with your oven. But if you
need to bake thousands of cakes, you would probably
build or acquire a special cake-baking machine, built for
making cakes in massive quantities.
 Similarly, when deciding whether to create a function for
reuse or simply writing in-line code, the most important
factor to consider is the extent to which it can save you
from writing repetitive code.
 If the code you are writing will be used more than once, it
is probably best to create a function to represent the code.
Functions In PHP
 A function is a self-contained block of code that can be
called by your script.
 When called (or invoked), the function’s code is executed
and performs a particular task. You can pass values to a
function (called arguments), which then uses the values
appropriately – storing them, transforming them,
displaying them, whatever the function is designed to do.
When finished, a function can also pass a value back to
the original code that called it into action.
 In PHP, functions come in two flavors – those built in to
the language, and those that you define yourself.
Functions In PHP
 PHP has hundreds of built-in functions. Consider the example shown on
the next page that utilizes the built-in function strtoupper().
 The output from this script is shown below:
Functions In PHP
Functions In PHP
 In the previous example, the function strtoupper() is
called and passed a variable whose value is represented by a
string. The function goes about its business of changing
the contents of the string to uppercase letters.
 A function call consists of the function name followed by
parentheses. (Note, even a function that has no parameters
requires a set of parentheses.) The information being
passed to the function (the arguments) are placed between
the parentheses.
 For functions that require more than one argument, the
arguments are separated by commas:
some_function ($an_argument, $another_argument);
Functions In PHP
 The strtoupper()from the previous example is typical
for a function in that it returns a value. Most functions
return some information back after they’ve completed their
task – they usually at least tell whether their mission was
successful.
 The strtoupper() function returns a string value so its
usage requires the presence of a variable to accept the
returned string, as was the case in the line:
$capitalized_string – strtoupper($original_string);
 Functions in PHP that return values use a return
statement within the body of the function. We’ll use this in
a few more pages when we start constructing our own
functions.
Defining Functions In PHP
 You can define your own functions in PHP using the
function statement:
function someFunction($argument1,. . .,argument2) {
//function code goes here
}
 The name of the function follows the function statement
and precedes a set of parentheses. If your function requires
arguments, you must place the comma-separated variable
names within the parentheses. These variables will be
filled by the values passed to your function when it is
called.
 Even if your function does not require arguments you must
still supply the parentheses.
Defining Functions In PHP
 Naming conventions for functions are the same as for
normal variables in PHP. As with variables you should apply
meaningful names and be consistent in naming and style.
Using mixed case in function names is a common
convention, thus myFunction() instead of
myfunction() or my_function(). (Note: variables
names are case sensitive in PHP, function names are not!)
 Let’s define a simple function that simply prints out the word
“Hello” in big letters.
function bigHello() {
echo “<h1> HELLO </h1>”
}
Defining Functions In PHP
Function
definition
Function call
Function result
Defining Functions With Argument
 For the next example, let’s define a function that requires an
argument. Actually, let’s define two different functions that
each take an argument.
 The first function will take a string and print the string with
a <br /> element appended to the string. The second
function will do the same, but append two <br />
elements to the end of the string.
Arrays &amp; functions in php
 For the next example, let’s define a function that requires two
arguments. We’ll basically repeat the exercise from the
previous example, but in this case rather than writing two
different functions that differ only in the number of <br />
elements they append to a line of text, the new function will
have a second argument that represents the number of <br />
elements to be appended. Clearly this would be more efficient,
in terms of code, than creating a different function for each
number of <br /> elements we might want to append.
 In the first version of this example, shown on the next page, I
simply repeated the same effect as in the previous version, so
the two screen shots from the browser should look identical.
 The second version of this example, shown on page 15, a
different effect is produced by the function calls.
Defining Functions With Two Arguments
Arrays &amp; functions in php
Arrays &amp; functions in php
Passing By Value To Functions
 When you pass arguments to functions, they are stored as
copies in parameter variables. This means that any changes
made to these variables by the function is local to the
function and are not reflected beyond it.
 The example on the next page illustrates argument passing by
value.
Upon return the
value of
$original_num is
unchanged by the
function.
Passing By Reference To Functions
 By default in PHP, variables passed to functions are passed by
value. In other words, only local copies of the variables are
used by the functions and the original values of the variables
are not accessible by the function.
 So how can you allow a function to actually modify a variable
sent to it? You must create a reference to the variable.
 The reference operator in PHP is the & (ampersand). Placing
an ampersand in front of an argument in a function definition
creates a reference to the variable and allows the function to
modify the original variable.
 The following example modifies the previous example to
make use of passing an argument by reference.
The argument $num is passed by
reference since it is preceded with
the & operator.
Upon return from the function the
value of $original_num has
been changed.
Arrays In PHP
 Most of our PHP examples to this point have involved
scalar variables (we did see a couple of example in the first
section of notes that made use of one of PHP’s global
associative arrays).
 Scalar variables can only hold a single value at a time. For
example, a variable $color could hold only a single
value such as red, at any point in time. The variable could
not be used to hold more than one color.
 Arrays are special types of variables that enable you to
store as many values as you want.
Note: Although you can technically make an array as large as you’d like, some built-in
array handling functions in PHP have an upper limit of 100,000 values. If you are storing
more data that this in your arrays and you need to use one of these functions, you will
either need to write your own function or split the data into multiple arrays.
Arrays In PHP
 Arrays are indexed, which means that each entry in the
array, called an element, is made up of a key and a value.
 The key is the index position, beginning with 0 and
increasing incrementally by 1 with each new element in
the array.
 The value is whatever value you associate with that
position – a string, an integer, or whatever you want.
 In PHP you can think of an array as a filing cabinet and
each key/value pair as a file folder. The key is the label
written on the tab of the folder, and the value is what is
inside. What’s inside each folder can vary from folder to
folder.
Creating Arrays In PHP
 You can create an array using either the array()
function or the array operator [].
 The array() function is usually used when you want to
create a new array and populate it with more than one
element, all at the same time.
 The array operator is more often used when you want to
create a new array with just one element at the outset or
when you want to add to an existing array element.
 The examples on the following couple of pages illustrate
creating an array in PHP using these two techniques.
This version uses the
array() function to
create the array.
This version uses the array
operator [ ] to create the array.
Note that no index values are
specified, PHP will auto number for
you
This version also uses the array
operator [ ] to create the array.
Note that index values are specified
in this case.
Creating Arrays In PHP
 As shown in the example on page 44, PHP can
automatically index the array for you when you use the [ ]
operator to create the array.
 This is useful in that it eliminates the possibility that you
might misnumber the elements. The example on the next
page illustrates what happens if you misnumber the
elements in an array.
Misnumbering starts
here with no element 4
defined and then 6 too
is missed.
Arrays &amp; functions in php
Creating Associative Arrays In PHP
 The arrays we’ve seen so far have been numerically
indexed, meaning that they use an integer index position
as the key.
 Associative arrays utilize actual named keys. In PHP, the
named keys of an associative array are character strings
rather than numerical values. The string value is used to
look up or provide a cross-reference to the data value.
Arrays &amp; functions in php
Iterating through an Array
 A common iterative statement used with both sequential
and associative arrays is the foreach statement.
 The general syntax of the foreach statement is:
foreach ( arrayname as variable ) {
. . . Statements to repeat
}
 The first variable inside the parentheses is the variable name
representing the array and the second variable is
automatically set to the next array item at each iteration of
the loop. An example using a sequential array is shown on
the next page and one with an associative array on the
following page.
Arrays &amp; functions in php
Arrays &amp; functions in php
Sorting Arrays In PHP
Arrays &amp; functions in php
Arrays &amp; functions in php
Arrays &amp; functions in php
Arrays &amp; functions in php
Arrays &amp; functions in php
Arrays &amp; functions in php
Array Functions
Arrays &amp; functions in php
Arrays &amp; functions in php
Examples of Some Array Functions
Arrays &amp; functions in php
Arrays &amp; functions in php
Arrays &amp; functions in php

More Related Content

What's hot (20)

PDF
Php Tutorials for Beginners
Vineet Kumar Saini
 
PPT
Class 2 - Introduction to PHP
Ahmed Swilam
 
PDF
Creating native apps with WordPress
Marko Heijnen
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PDF
Functions in PHP
Vineet Kumar Saini
 
ODP
PHP Basic
Yoeung Vibol
 
ODP
PHP Web Programming
Muthuselvam RS
 
PPT
Php i basic chapter 3
Muhamad Al Imran
 
PPTX
Dev traning 2016 basics of PHP
Sacheen Dhanjie
 
PDF
PHP 8.1 - What's new and changed
Ayesh Karunaratne
 
PPT
PHP - Introduction to PHP
Vibrant Technologies & Computers
 
PPT
PHP Workshop Notes
Pamela Fox
 
PPT
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 
PPT
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
PPT
Basic PHP
Todd Barber
 
PPT
Introduction To Lamp
Amzad Hossain
 
PPTX
php basics
Anmol Paul
 
PPT
Introduction to PHP
Jussi Pohjolainen
 
PPT
PHP
sometech
 
Php Tutorials for Beginners
Vineet Kumar Saini
 
Class 2 - Introduction to PHP
Ahmed Swilam
 
Creating native apps with WordPress
Marko Heijnen
 
PHP FUNCTIONS
Zeeshan Ahmed
 
Functions in PHP
Vineet Kumar Saini
 
PHP Basic
Yoeung Vibol
 
PHP Web Programming
Muthuselvam RS
 
Php i basic chapter 3
Muhamad Al Imran
 
Dev traning 2016 basics of PHP
Sacheen Dhanjie
 
PHP 8.1 - What's new and changed
Ayesh Karunaratne
 
PHP - Introduction to PHP
Vibrant Technologies & Computers
 
PHP Workshop Notes
Pamela Fox
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Basic PHP
Todd Barber
 
Introduction To Lamp
Amzad Hossain
 
php basics
Anmol Paul
 
Introduction to PHP
Jussi Pohjolainen
 

Viewers also liked (20)

PPT
PHP Tutorials
Yuriy Krapivko
 
PPTX
PHP for HTML Gurus - J and Beyond 2012
Andrea Tarr
 
PDF
Memphis php html form processing with php
Joe Ferguson
 
PPT
Chapter 02 php basic syntax
Dhani Ahmad
 
PPT
Oops concepts in php
CPD INDIA
 
PPTX
Document object model(dom)
rahul kundu
 
PPTX
5 random variables
Zahida Pervaiz
 
PDF
Javascript and DOM
Brian Moschel
 
PPT
09 numerical integration
Mohammad Tawfik
 
PDF
Probability mass functions and probability density functions
Ankit Katiyar
 
PPT
07 interpolation
Mohammad Tawfik
 
PPTX
Gauss jordan
jorgeduardooo
 
PDF
Math1003 1.13 - Significant Digits, Accuracy, Precision
gcmath1003
 
PPTX
Polynomial regression
naveedaliabad
 
PPTX
Regression
RAVI PRASAD K.J.
 
PPTX
Linear programming graphical method (feasibility)
Rajesh Timane, PhD
 
PPTX
Probability Density Function (PDF)
AakankshaR
 
PPTX
Uniform Distribution
mathscontent
 
PPT
Newton raphson method
Bijay Mishra
 
DOC
Ordinary least squares linear regression
Elkana Rorio
 
PHP Tutorials
Yuriy Krapivko
 
PHP for HTML Gurus - J and Beyond 2012
Andrea Tarr
 
Memphis php html form processing with php
Joe Ferguson
 
Chapter 02 php basic syntax
Dhani Ahmad
 
Oops concepts in php
CPD INDIA
 
Document object model(dom)
rahul kundu
 
5 random variables
Zahida Pervaiz
 
Javascript and DOM
Brian Moschel
 
09 numerical integration
Mohammad Tawfik
 
Probability mass functions and probability density functions
Ankit Katiyar
 
07 interpolation
Mohammad Tawfik
 
Gauss jordan
jorgeduardooo
 
Math1003 1.13 - Significant Digits, Accuracy, Precision
gcmath1003
 
Polynomial regression
naveedaliabad
 
Regression
RAVI PRASAD K.J.
 
Linear programming graphical method (feasibility)
Rajesh Timane, PhD
 
Probability Density Function (PDF)
AakankshaR
 
Uniform Distribution
mathscontent
 
Newton raphson method
Bijay Mishra
 
Ordinary least squares linear regression
Elkana Rorio
 
Ad

Similar to Arrays &amp; functions in php (20)

PPTX
PHP FUNCTIONS AND ARRAY.pptx
ShaliniPrabakaran
 
PPTX
function in php like control loop and its uses
vishal choudhary
 
PPTX
function in php using like three type of function
vishal choudhary
 
PPT
PHP-03-Functions.ppt
Jamers2
 
PPT
PHP-03-Functions.ppt
ShishirKantSingh1
 
PDF
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
PPTX
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
PDF
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
PPTX
Functions in PHP.pptx
Japneet9
 
PDF
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
PPTX
Php
Yoga Raja
 
PPTX
UNIT- 2 Functions__________________.pptx
harleensingh985
 
PPTX
Web Application Development using PHP Chapter 3
Mohd Harris Ahmad Jaal
 
PPT
Web Technology_10.ppt
Aftabali702240
 
PPTX
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
PDF
Php, mysq lpart3
Subhasis Nayak
 
PDF
Web Design EJ3
Aram Mohammed
 
PPTX
The basics of php for engeneering students
rahuljustin77
 
PPTX
php Chapter 1.pptx
HambaAbebe2
 
PHP FUNCTIONS AND ARRAY.pptx
ShaliniPrabakaran
 
function in php like control loop and its uses
vishal choudhary
 
function in php using like three type of function
vishal choudhary
 
PHP-03-Functions.ppt
Jamers2
 
PHP-03-Functions.ppt
ShishirKantSingh1
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
Functions in PHP.pptx
Japneet9
 
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
UNIT- 2 Functions__________________.pptx
harleensingh985
 
Web Application Development using PHP Chapter 3
Mohd Harris Ahmad Jaal
 
Web Technology_10.ppt
Aftabali702240
 
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
Php, mysq lpart3
Subhasis Nayak
 
Web Design EJ3
Aram Mohammed
 
The basics of php for engeneering students
rahuljustin77
 
php Chapter 1.pptx
HambaAbebe2
 
Ad

Recently uploaded (20)

PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Dimensions of Societal Planning in Commonism
StefanMz
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 

Arrays &amp; functions in php

  • 2. Functions In PHP  Functions are at the heart of a well-organized script and will make your code easy to read and reuse.  Large projects would be unmanageable without functions because the problem of repetitive code would bog down the development process.  If you haven’t had much experience using functions, you can think of a function as an input/output machine. The machine takes the raw materials you feed it (the input) and works with them to produce a product (the output).  A function accepts values, processes them, and then performs an action (printing to the browser, for example), returns a new value, or both.
  • 3. Functions In PHP  If you need to bake a cake, you would probably do it yourself, in your own kitchen with your oven. But if you need to bake thousands of cakes, you would probably build or acquire a special cake-baking machine, built for making cakes in massive quantities.  Similarly, when deciding whether to create a function for reuse or simply writing in-line code, the most important factor to consider is the extent to which it can save you from writing repetitive code.  If the code you are writing will be used more than once, it is probably best to create a function to represent the code.
  • 4. Functions In PHP  A function is a self-contained block of code that can be called by your script.  When called (or invoked), the function’s code is executed and performs a particular task. You can pass values to a function (called arguments), which then uses the values appropriately – storing them, transforming them, displaying them, whatever the function is designed to do. When finished, a function can also pass a value back to the original code that called it into action.  In PHP, functions come in two flavors – those built in to the language, and those that you define yourself.
  • 5. Functions In PHP  PHP has hundreds of built-in functions. Consider the example shown on the next page that utilizes the built-in function strtoupper().  The output from this script is shown below:
  • 7. Functions In PHP  In the previous example, the function strtoupper() is called and passed a variable whose value is represented by a string. The function goes about its business of changing the contents of the string to uppercase letters.  A function call consists of the function name followed by parentheses. (Note, even a function that has no parameters requires a set of parentheses.) The information being passed to the function (the arguments) are placed between the parentheses.  For functions that require more than one argument, the arguments are separated by commas: some_function ($an_argument, $another_argument);
  • 8. Functions In PHP  The strtoupper()from the previous example is typical for a function in that it returns a value. Most functions return some information back after they’ve completed their task – they usually at least tell whether their mission was successful.  The strtoupper() function returns a string value so its usage requires the presence of a variable to accept the returned string, as was the case in the line: $capitalized_string – strtoupper($original_string);  Functions in PHP that return values use a return statement within the body of the function. We’ll use this in a few more pages when we start constructing our own functions.
  • 9. Defining Functions In PHP  You can define your own functions in PHP using the function statement: function someFunction($argument1,. . .,argument2) { //function code goes here }  The name of the function follows the function statement and precedes a set of parentheses. If your function requires arguments, you must place the comma-separated variable names within the parentheses. These variables will be filled by the values passed to your function when it is called.  Even if your function does not require arguments you must still supply the parentheses.
  • 10. Defining Functions In PHP  Naming conventions for functions are the same as for normal variables in PHP. As with variables you should apply meaningful names and be consistent in naming and style. Using mixed case in function names is a common convention, thus myFunction() instead of myfunction() or my_function(). (Note: variables names are case sensitive in PHP, function names are not!)  Let’s define a simple function that simply prints out the word “Hello” in big letters. function bigHello() { echo “<h1> HELLO </h1>” }
  • 11. Defining Functions In PHP Function definition Function call Function result
  • 12. Defining Functions With Argument  For the next example, let’s define a function that requires an argument. Actually, let’s define two different functions that each take an argument.  The first function will take a string and print the string with a <br /> element appended to the string. The second function will do the same, but append two <br /> elements to the end of the string.
  • 14.  For the next example, let’s define a function that requires two arguments. We’ll basically repeat the exercise from the previous example, but in this case rather than writing two different functions that differ only in the number of <br /> elements they append to a line of text, the new function will have a second argument that represents the number of <br /> elements to be appended. Clearly this would be more efficient, in terms of code, than creating a different function for each number of <br /> elements we might want to append.  In the first version of this example, shown on the next page, I simply repeated the same effect as in the previous version, so the two screen shots from the browser should look identical.  The second version of this example, shown on page 15, a different effect is produced by the function calls. Defining Functions With Two Arguments
  • 17. Passing By Value To Functions  When you pass arguments to functions, they are stored as copies in parameter variables. This means that any changes made to these variables by the function is local to the function and are not reflected beyond it.  The example on the next page illustrates argument passing by value.
  • 18. Upon return the value of $original_num is unchanged by the function.
  • 19. Passing By Reference To Functions  By default in PHP, variables passed to functions are passed by value. In other words, only local copies of the variables are used by the functions and the original values of the variables are not accessible by the function.  So how can you allow a function to actually modify a variable sent to it? You must create a reference to the variable.  The reference operator in PHP is the & (ampersand). Placing an ampersand in front of an argument in a function definition creates a reference to the variable and allows the function to modify the original variable.  The following example modifies the previous example to make use of passing an argument by reference.
  • 20. The argument $num is passed by reference since it is preceded with the & operator. Upon return from the function the value of $original_num has been changed.
  • 21. Arrays In PHP  Most of our PHP examples to this point have involved scalar variables (we did see a couple of example in the first section of notes that made use of one of PHP’s global associative arrays).  Scalar variables can only hold a single value at a time. For example, a variable $color could hold only a single value such as red, at any point in time. The variable could not be used to hold more than one color.  Arrays are special types of variables that enable you to store as many values as you want. Note: Although you can technically make an array as large as you’d like, some built-in array handling functions in PHP have an upper limit of 100,000 values. If you are storing more data that this in your arrays and you need to use one of these functions, you will either need to write your own function or split the data into multiple arrays.
  • 22. Arrays In PHP  Arrays are indexed, which means that each entry in the array, called an element, is made up of a key and a value.  The key is the index position, beginning with 0 and increasing incrementally by 1 with each new element in the array.  The value is whatever value you associate with that position – a string, an integer, or whatever you want.  In PHP you can think of an array as a filing cabinet and each key/value pair as a file folder. The key is the label written on the tab of the folder, and the value is what is inside. What’s inside each folder can vary from folder to folder.
  • 23. Creating Arrays In PHP  You can create an array using either the array() function or the array operator [].  The array() function is usually used when you want to create a new array and populate it with more than one element, all at the same time.  The array operator is more often used when you want to create a new array with just one element at the outset or when you want to add to an existing array element.  The examples on the following couple of pages illustrate creating an array in PHP using these two techniques.
  • 24. This version uses the array() function to create the array.
  • 25. This version uses the array operator [ ] to create the array. Note that no index values are specified, PHP will auto number for you
  • 26. This version also uses the array operator [ ] to create the array. Note that index values are specified in this case.
  • 27. Creating Arrays In PHP  As shown in the example on page 44, PHP can automatically index the array for you when you use the [ ] operator to create the array.  This is useful in that it eliminates the possibility that you might misnumber the elements. The example on the next page illustrates what happens if you misnumber the elements in an array.
  • 28. Misnumbering starts here with no element 4 defined and then 6 too is missed.
  • 30. Creating Associative Arrays In PHP  The arrays we’ve seen so far have been numerically indexed, meaning that they use an integer index position as the key.  Associative arrays utilize actual named keys. In PHP, the named keys of an associative array are character strings rather than numerical values. The string value is used to look up or provide a cross-reference to the data value.
  • 32. Iterating through an Array  A common iterative statement used with both sequential and associative arrays is the foreach statement.  The general syntax of the foreach statement is: foreach ( arrayname as variable ) { . . . Statements to repeat }  The first variable inside the parentheses is the variable name representing the array and the second variable is automatically set to the next array item at each iteration of the loop. An example using a sequential array is shown on the next page and one with an associative array on the following page.
  • 45. Examples of Some Array Functions