Tải bản đầy đủ (.ppt) (29 trang)

Slide môn học PHP session 4 functions in PHP

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (357.5 KB, 29 trang )

Functions in PHP
Session 11


Review - I




Loop is executed depending on the return value of the
testing conditions by testing it
Different types of looping statements hold the
iterations of the loops. The different types of looping
statements are:
 While Loop
 Do-While Loop
 For Loop

PHP / Session 11 / Slide 2 of 29


Review - II








While loop executes loop statements depending on the return


result of the testing condition after testing it
Testing condition in Do-While loop is placed after the loop
ends
For loop executes a set of codes repetitively for a specified
amount of time
Execution of the loops is controlled with the help of jump
statements such as:
 Break Statement
 Continue Statement
 Exit Statement
PHP / Session 11 / Slide 3 of 29


Review - III






Break statement stops the iteration of the loop from
the current loop execution
Continue statement is used for breaking the iteration
of the loop from the current loop execution
Exit statement is used to break the loop while the loop
is in the execution process

PHP / Session 11 / Slide 4 of 29



Objectives


Use Functions



Use built in functions



Use user defined function



Pass arguments to function



Return values from functions



Use recursive functions

PHP / Session 11 / Slide 5 of 29


Functions








Function is block of code that has performs a specified
job
Function enables reusing the same piece of code in the
program without typing it again
Function helps us in making a program organized
Functions enhance the logical flow in a program by
dividing up complicated code sequences into smaller
modules

PHP / Session 11 / Slide 6 of 29


Naming Conventions for
Functions




A valid function name may contain:
 Characters from A-Z
 Numbers form 0-9. A function name cannot start with a
number.
 Underscore
A function name cannot contain the following:

 Blank Spaces
 Periods
 Reserved words
 Syntactical punctuation
PHP / Session 11 / Slide 7 of 29


Built - in Functions





PHP has built in functions within it
Built in functions can work with databases, numbers,
strings, arrays, dates and time, and email
Whenever the function is been called they are
compiled directly in PHP

PHP / Session 11 / Slide 8 of 29


Mathematical Functions - I


Mathematical functions operate on numerical data.
Following table lists few of the built in mathematical
functions available:
Function


General Form

Description

abs

abs(arg)

Returns the absolute value of the argument

max

max(arg1,arg2,…
argn)
max(array[])

Returns the largest value of the specified
arguments. It also allows comparing
multiple arrays. For multiple arrays, max
function compares from left to right

PHP / Session 11 / Slide 9 of 29


Mathematical Functions - II
Function

General Form

Description


min

min(ar1,
Returns the smallest value of the argument.
arg2,..argn It also allows comparing multiple arrays.
)
For multiple arrays, main function compares
form left to right.

sqrt

sqrt(float
arg)

pow

pow(number Returns the value of base raised to the
base,
power of exp
number exp)

Returns the square root of argument

PHP / Session 11 / Slide 10 of 29


Mathematical Functions - III
Function


General Form

Description

pow

pow(number
base, number
exp)

Returns the value of base raised to the
power of exp

round

round(float
val [,int
precision)

Returns the rounded value of the
argument specified

PHP / Session 11 / Slide 11 of 29


String Functions - I



String functions operate on character type of data

Following table lists few of the string functions available in PHP :
Function

General Form

Description

chr

chr(int ascii)

Returns
the
character
equivalent to the specified as
the ASCII

bin2hex

bin2hex(string)

Returns ASCII value. This
ASCII value is the hexadecimal
representation of the string.

strtolower strtolower(string) Converts the string entered as
the argument into lower
case
PHP / Session 11 / Slide 12 of 29



String Functions - II
Function
chr

General Form
chr(int
ascii)

Description
Returns the character equivalent to the
specified as the ASCII

bin2hex bin2hex(strin
g)

Returns ASCII value. This ASCII value
is the hexadecimal representation of the
string.

strtolo strtolower(st
wer
ring)

Converts the string entered as the
argument into lower case
PHP / Session 11 / Slide 13 of 29


String Functions - III

Function

General Form

Description

strlen

strlen(string)

strcmp

strcmp(string1,stri Returns zero if the string1 is
ng2)
equal to string2. It returns less
than zero, if the string1 is less
than the string2. Otherwise, it
returns greater than zero.

Returns the length of the string
specified as argument

PHP / Session 11 / Slide 14 of 29


String Functions - IV
Function

ord


General Form

ord(string)

Description

Returns the ASCII value of
the first character of the
string. The ord function is the
complement of the chr
function.

PHP / Session 11 / Slide 15 of 29


Date and Time Functions


The date and time functions enable us to obtain the date and time from the
system. Following table lists few of the date functions available:

Function

General Form

Description

checkdate

checkdate(month,da

y,year)

Returns the value as TRUE,
only if the given value is
valid. It will return a FALSE
value, if the date is invalid.

getdate

getdate(timestamp)

Parses an English datetime
format
into
a
UNIX
timestamp
PHP / Session 11 / Slide 16 of 29


Date and Time Functions - II
Function
time

General Form
time(void)

strtotime Strtotime(string
time [,int now])


Description
Provides the current time
measured in the number of
seconds
Accepts a string as the
argument and formats it into
a Unix timestamp relative to
specified in the now
argument

PHP / Session 11 / Slide 17 of 29


Error Handling Functions- I



Error handling functions deal with errors
They allow user to define the error handling rules
Function
debug_backtrace

General Form

Description

debug_backtrace(vo Generates a PHP
id)
backtrace


set_error_handler set_error_handler(
callback
error_handler
[,int error_types]

Sets
a
user
function
to
handle errors in a
script

PHP / Session 11 / Slide 18 of 29


Error Handling Functions - II
Function

General Form

error_reporting error_reporting( C
onstant)
trigger_error

trigger_error(stri
ng error_msg [,int
error type]

Description

Sets which PHP errors
are reported
Used to activate a user
error condition. This
function is useful
when we want to
generate a particular
response to some error
at run time
PHP / Session 11 / Slide 19 of 29


Declaring a Function



A user can also define a function for performing a task
To declare a function, the syntax is:
function fun_name()
{
code
}
Where,
 function - is a keyword that specifies to PHP that the
code following it is function
 fun_name - Specifies the name of the function
 code - Defines the valid PHP code
PHP / Session 11 / Slide 20 of 29



Passing arguments to function - I



A user may pass arguments to a function
A user may pass values into three ways. They are as
follows:
 Passing by value
 Passing by reference
 Setting default values

PHP / Session 11 / Slide 21 of 29


Passing arguments to function - II


To define the function for passing parameters:
function fun_name(arg1,arg2,...,argn)
{
code
}
Where,
 fun_name - Specifies the name of the function
 arg - Specifies the arguments that are passed to the
function
 code - Defines a valid PHP code
PHP / Session 11 / Slide 22 of 29



Passing Argument by value






Value of the argument remains unchanged outside
the function if we pass the arguments by value
If we wish to allow a function to modify its
arguments, we must pass them by reference
When a function is defined, the $ sign is used with
the arguments to indicate that the argument is
passed by value

PHP / Session 11 / Slide 23 of 29


Passing Value by reference





We can also pass the value by reference
If the value is passed by reference then the value is
modified even outside the function
When the function is defined, the ‘&’ sign is used
along with the parameters to indicate that the value is
passed by reference


PHP / Session 11 / Slide 24 of 29


Setting default parameters


We can also assign default values to the parameters



We assign a default value to the argument at the place
where the function is declared

PHP / Session 11 / Slide 25 of 29


×