Following table lists few of the built in mathematical functions available: Function General Form Description abs absarg Returns the absolute value of the argument max maxarg1,arg2,… arg
Trang 1Functions in PHP
Session 11
Trang 2 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
Trang 3 Execution of the loops is controlled with the help of jump
statements such as:
Break Statement
Continue Statement
Exit Statement
Trang 4Review - 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
Trang 5 Use Functions
Use built in functions
Use user defined function
Pass arguments to function
Return values from functions
Use recursive functions
Trang 6 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
Trang 7Naming 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.
Trang 8Built - 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
Trang 9Mathematical 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
Trang 10Mathematical Functions - II
Function General Form Description
min min(ar1,
arg2, argn)
Returns the smallest value of the argument
It also allows comparing multiple arrays For multiple arrays, main function compares form left to right
Returns the value of base raised to the power of exp
Trang 11Mathematical 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
Trang 12String 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
Trang 13Returns ASCII value This ASCII value
is the hexadecimal representation of the string
strtolo strtolower(st Converts the string entered as the
Trang 14String Functions - III
Function General Form Description
strlen strlen(string) Returns the length of the string
specified as argumentstrcmp strcmp(string1,stri
ng2)
Returns zero if the string1 is equal to string2 It returns less than zero, if the string1 is less than the string2 Otherwise, it returns greater than zero
Trang 15String Functions - IV
Function General Form Description
ord ord(string) Returns the ASCII value of
the first character of the string The ord function is the complement of the chr function
Trang 16Date and Time Functions
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
Trang 17Date and Time Functions - II
Function General Form Description
time time(void) Provides the current time
measured in the number of seconds
strtotime Strtotime(string
time [,int now])
Accepts a string as the argument and formats it into
a Unix timestamp relative to specified in the now
argument
Trang 18Error Handling Functions- I
Error handling functions deal with errors
They allow user to define the error handling rules
Function General Form Description
debug_backtrace debug_backtrace(vo
id)
Generates a PHP backtrace
set_error_handler set_error_handler(
callback error_handler [,int error_types]
Sets a user function to handle errors in a script
Trang 19Error Handling Functions - II
Function General Form Description
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
Trang 20Declaring a Function
A user can also define a function for performing a task
To declare a function, the syntax is:
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
Trang 21Passing arguments to function - I
A user may pass arguments to a function
A user may pass values into three ways They are as
Trang 22Passing arguments to function - II
To define the function for passing parameters:
fun_name - Specifies the name of the function
arg - Specifies the arguments that are passed to the
function
code - Defines a valid PHP code
Trang 23Passing 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
Trang 24Passing 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
Trang 25Setting 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
Trang 26Returning values from functions
A function can also return values
The return statement is used to return value from
the function
Trang 27Nesting of Functions
A function enables to divide a program into modules
These modules may be interdependent and may
require interaction between them
When one function calls several other functions, it is
termed nesting of functions
Trang 28 When a function calls itself, it is termed as recursion
Advantage:
Simpler to code compared to non recursive functions,
which are implemented using loops
Easy to understand at a later stage
Disadvantage:
May require more time to execute as against the non
recursive functions
Trang 29 Functions are used to avoid rewriting the codes again and again
We have built-in functions in PHP
The built-in functions can be categorized as:
Mathematical Functions
String Functions
Date and Time Functions
Error Handling Functions
A user can also define a function
We can pass arguments to a function
We can pass arguments to a function by value, reference, or even pass default values to a function