3. Function
• Sub routine.
• A function is a block of code that you can call
throughout your PHP script. Functions are
often used for common functionalities that
can occur in your PHP program. For an
example think that you write a program that
relates to seasonal offers. In many occasions,
you would need to know the final price after a
discount was given. You can define a function
for this as below.
4. Creating function
function functionName($param1, $param2)
{
code to be executed;
code to be executed;
return anyValue;
}
Optionally, datas to be
passed into function. It
can be unlimited
number of datas.
Optionally return
value back to the
one who call the
function.
5. Creating function- Example
function print_hello()
{
print “Hello”;
}
A function that does not require any external
datas to process. It done the job by just print
word “Hello” to the screen. It return nothing
to the place it is being called.
6. Creating function- Example
function transfer($account_a, $account_b, $amount)
{
$account_a = $account_a - $amount;
$account_b = $account_b + $amount;
return $account_b;
}
• A function that takes 3 external datas (parameters)
to process. The function subtract $amount from
$account_a then $amount add to $account_b. Finally
return the result to the place it is being called.
7. Calling function
/*** Functions declaration ***/
function print_hello()
{
print “Hello”;
}
function transfer($account_a, $account_b, $amount)
{
$account_a = $account_a - $amount;
$account_b = $account_b + $amount;
return $account_b;
}
/*** Using functions we just declare above here ***/
print_hello();
print transfer(500, 200, 50);
9. Useful PHP Built-in functions
• (Q) What is PHP built-in functions?
• (A) Instant, Ready to use functions provided
by PHP producer. No need to create by
ourself.
11. Function for String :: trim()
Benefit: Remove whitespace from both ends of
string
Example:
$str = ‘ Hello There ’;
$new_str = trim($str);
print $new_str;
Result:
Hello There
13. Function for String :: implode()
Benefit: Implode array into string
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
print $comma_separated; // lastname,email,phone
14. Function for String :: strlen()
Benefit: Get string’s length (How many characters inside
the string)
$str = 'abcdef';
print strlen($str); // 6
$str = ' ab cd ';
print strlen($str); // 7
15. Function for String :: strstr()
Benefit: Find the first occurrence in the string
case-sensitive stristr() will be used for incase-
sensitive.
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
17. Function for Array :: array_rand()
Benefit: Pick one or more random entries out of an array
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
print $rand_keys;
18. Function for Array :: print_r()
Benefit: Print structure of array.
Note that we should wrap it with
<pre></pre> for readability.
$a = array ('a' => 'apple',
'b' => 'banana',
'c' => array ('x', 'y', 'z'));
print “<pre>”;
print_r($a);
print “<pre>”;
/****
<pre>
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
</pre>
****/