W7 Lesson 6 PHP Functions, Array and Strings - Module
W7 Lesson 6 PHP Functions, Array and Strings - Module
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!";
}
?>
setHeight(88);
setHeight(45);
?>
PHP Functions - Returning values
To let a function, return a value, use the return statement:
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>
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
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Output:
Peter is 35 years old.
Output:
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
[Type Subject / Course title here]
19
[Type Topic here]
Arrays
Course Module
[Type Subject / Course title here]
21
[Type Topic here]
Sorting Arrays
Course Module
[Type Subject / Course title here]
23
[Type Topic here]
Course Module
Loading Array from a Directory
[Type Subject / Course title here]
25
[Type Topic here]
Finding Text
Course Module
[Type Subject / Course title here]
27
[Type Topic here]
Course Module
[Type Subject / Course title here]
29
[Type Topic here]
Course Module
[Type Subject / Course title here]
31
[Type Topic here]
References
Murach, J. (2014) Murach’s PHP and MYSQL (2 nd Edition)
WEBSITE
http://php.net/
http://www.w3schools.com/php/
https://www.tutorialspoint.com/php/
Course Module