PHP Functions, Array and Strings PDF
PHP Functions, Array and Strings PDF
1
[Type Topic here]
Objectives:
PHP FUNCTIONS
Course Module
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:
<?php
function writeMsg() {
echo "Hello world!";
}
?>
Simple function
Course Module
Function and display data
[Type Subject / Course title here]
5
[Type Topic here]
Course Module
Function with display data in and out
[Type Subject / Course title here]
7
[Type Topic here]
Course Module
[Type Subject / Course title here]
9
[Type Topic here]
Course Module
Function to return multiple values
[Type Subject / Course title here]
11
[Type Topic here]
Course Module
[Type Subject / Course title here]
13
[Type Topic here]
Using Includes
Course Module
[Type Subject / Course title here]
15
[Type Topic here]
Course Module
PHP Arrays
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";
To loop through and print all the values of an indexed array, you could
use a for loop, like this:
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
Course Module
The named keys can then be used in a script:
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Output:
Peter is 35 years old.
Output:
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
Arrays
[Type Subject / Course title here]
19
[Type Topic here]
Course Module
Sorting Arrays
[Type Subject / Course title here]
21
[Type Topic here]
Course Module
Loading array from a file
[Type Subject / Course title here]
23
[Type Topic here]
Course Module
Finding Text
[Type Subject / Course title here]
25
[Type Topic here]
Course Module
Using Regular Expression
[Type Subject / Course title here]
27
[Type Topic here]
Course Module
[Type Subject / Course title here]
29
[Type Topic here]
Course Module
References
Murach, J. (2014) Murach’s PHP and MYSQL (2nd Edition)
WEBSITE
http://php.net/
http://www.w3schools.com/php/
https://www.tutorialspoint.com/php/