php 2 Function creating, calling,PHP built-in function

18

Click here to load reader

Transcript of php 2 Function creating, calling,PHP built-in function

Page 1: php 2 Function creating, calling,PHP built-in function

PHP

Page 2: php 2 Function creating, calling,PHP built-in function

Function creating, calling,PHP built-in function

Page 3: php 2 Function creating, calling,PHP built-in function

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.

Page 4: php 2 Function creating, calling,PHP built-in function

Creating function

function functionName($param1, $param2){

code to be executed;code to be executed;return anyValue;

}

Optionally, datas to bepassed into function. It can be unlimited number of datas.

Optionally return value back to the one who call the function.

Page 5: php 2 Function creating, calling,PHP built-in function

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.

Page 6: php 2 Function creating, calling,PHP built-in function

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.

Page 7: php 2 Function creating, calling,PHP built-in function

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);

Page 8: php 2 Function creating, calling,PHP built-in function

PHP Built-in Functionsin common use

Page 9: php 2 Function creating, calling,PHP built-in function

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.

Page 10: php 2 Function creating, calling,PHP built-in function

phpinfo()

Benefit: Print PHP information, configuration, settings out to screen

Example:phpinfo();

Page 11: php 2 Function creating, calling,PHP built-in function

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

Page 12: php 2 Function creating, calling,PHP built-in function

Function for String :: explode()

Benefit: Explode string into array

$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";$pieces = explode(" ", $pizza);print $pieces[0]; // piece1print $pieces[1]; // piece2

Page 13: php 2 Function creating, calling,PHP built-in function

Function for String :: implode()

Benefit: Implode array into string

$array = array('lastname', 'email', 'phone');$comma_separated = implode(",", $array);print $comma_separated; // lastname,email,phone

Page 14: php 2 Function creating, calling,PHP built-in function

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

Page 15: php 2 Function creating, calling,PHP built-in function

Function for String :: strstr()

Benefit: Find the first occurrence in the string case-sensitive stristr() will be used for incase-sensitive.

$email = '[email protected]';$domain = strstr($email, '@');echo $domain; // prints @example.com

Page 16: php 2 Function creating, calling,PHP built-in function

Function for Array :: count()Benefit: Count all elements in array

$a = array();$a[0] = 1;$a[1] = 3;$a[2] = 5;$result = count($a);print $result; // output 3$food = array('fruits' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea'));// recursive countprint count($food, COUNT_RECURSIVE); // output 8// normal countprint count($food); // output 2

Page 17: php 2 Function creating, calling,PHP built-in function

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;

Page 18: php 2 Function creating, calling,PHP built-in function

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>****/