PHP FUNCTION
FUNCTION
A function is a set of program statements that
perform a specific task, and that can be called, or
executed, from anywhere in your program.
Syntax
function function_name(arg1,arg2...argN){
block of statements;
return;
}
CALLING FUNCTIONS
Syntax
functionName()
functionName( argument )
functionName( argument1, argument2 )
EXAMPLE
function add_footer() {
echo "<img src=\" footer_logo.jpg\" width=\"100%\"
height=\"7\">";
echo "Pangasinan State University<br/>";
echo "<br/>Mc Arthur Highway";
echo "<br />Urdaneta City";
echo "<p>or send questions to";
echo "<a href=mailto:[email protected]> webmaster</a><br/>";
echo "<img src=\"footer_logo.jpg\" width=\"100%\" height=\"7\">";
return;
}
add_footer()
<img src=" footer_logo.jpg" width="100%" height="7">
Pangasinan State University
<br/>Mc Arthur Highway
<br />Urdaneta City
<p>or send questions to<a href=mailto:
[email protected]>webmaster</a><br/>
<img src="footer_logo.jpg" width="100%" height="7">
ARGUMENTS AND RETURN VALUES
Information can be passed to functions through
arguments. An argument is just like a variable.
A function often accepts one or more arguments , which
are values passed to the function by the code that calls it.
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.
To let a function return a value, use the return statement
ARGUMENTS AND RETURN VALUES
function square ($x) {
return $x*$x;
}
echo “The square of 5 is “. square(5);
function payment_method($cash_on_hand, $amount) {
if ($amount > $cash_on_hand) {
return "credit card";
} else {
return "cash";
}
}
echo "Mode of Payment: ".payment_method(500, 300);
UNDERSTANDING VARIABLE SCOPE
The scope of a variable is the part of the script
where the variable can be referenced/used.
Global
Local
LOCAL VARIABLES
function describeMyDog() {
$color = "brown";
echo "My dog is $color <br/> ";
}
$color = "black";
describeMyDog();
echo "My cat is $color <br/>";
GLOBAL
function studentName() {
global $studName;
$studFname = "Ghen";
$studLname = "Lomibao";
$studName=$studLname.", ".$studFname;
}
studentName();
echo $studName;
ARRAY
ANATOMY OF AN ARRAY
Array
An array stores a group of values in a series of
elements under a single variable name. It elements are
represented by a maps keys (or indexes) to values.
Three Types of Arrays
Indexed arrays
Associative arrays
Multidimensional arrays
CREATING ARRAYS
Example1
$authors = array("Steinbeck", "Kafka", "Tolkien", "Dickens" );
Example
$hayop['A'] = "pusa";
$hayop['B'] = "aso";
$hayop['C'] = "baka";
Example3
$hayop[] = "pusa";
$hayop[] = "aso";
$hayop[] = "baka";
Example4
$myBook = array("title" = > "The Grapes of Wrath", "author" = > "John
Steinbeck", "pubYear" = > 1939 );
ACCESSING ARRAY ELEMENTS
Example1
$authors = array( “Steinbeck”, “Kafka”, “Tolkien”,
“Dickens” );
$myAuthor = $authors[0];
$anotherAuthor = $authors[1];
Example2
$myBook = array("title" = > "The Grapes of Wrath", "author"
= > "John Steinbeck", "pubYear" = > 1939 );
$myTitle = $myBook["title"];
$myAuthor = $myBook["author"];
LOOPING THROUGH ARRAYS
$authors = array("Steinbeck", "Kafka", "Tolkien",
"Dickens" );
foreach ( $authors as $val ) {
echo "$val . < br/ > ";
}
$myBook = array("title" => "The Grapes of Wrath", "author" =>
"John Steinbeck","pubYear" => 1939 );
foreach ( $myBook as $key = > $value ) {
echo $key. " ".,$value;
}
LOOPING THROUGH ARRAYS
$authors = array("Steinbeck", "Kafka", "Tolkien",
"Dickens" );
foreach ( $authors as $val ) {
echo "$val . < br/> ";
}
$myBook = array("title" => "The Grapes of Wrath", "author" =>
"John Steinbeck","pubYear" => 1939 );
foreach ( $myBook as $key = > $value ) {
echo $key. " ".,$value;
}
LOOPING THROUGH ARRAYS
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
MULTIDIMENSIONAL ARRAYS
An array of arrays also called nested arrays.
Example
$myBook=array(array("title"=>"The Grapes of
Wrath","author"=>"John
Steinbeck","year"=>1979),array("title"=>"The
Trial","author"=>"Franz
Kafka","year"=>1925),array("title"=>"The
Hobbit","author"=>"J. R. R.
Tolkien","year"=>1937));
ARRAY FUNCTIONS
array_push()
array_pop()
sort()
implode()
explode()
ARRAY FUNCTIONS
array_push
Inserts one or more elements onto the end of array
Example
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
ARRAY FUNCTIONS
array_pop
Deletes the last element of an array
Example
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
ARRAY FUNCTIONS
sort
Sort an array
Example
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . “<br/>";
}
ARRAY FUNCTIONS
implode
Join array elements with a string
Example
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated;
ARRAY FUNCTIONS
explode
Split a string by string
Returns an array of strings
Example
$string = 'lastname, email, phone';
$array = explode(",", $string);
print_r($array);
REFERENCES
php.net
tutorialrepublic.com/php-tutorial
Tutorialspoint.com/php/index.html
w3schools.com/php