PHP Functions - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

PHP Functions

Description:

Returns an array in which each element is the corresponding member of the ... Values are returned by using the return statement. ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 13
Provided by: capi7
Category:
Tags: php | functions | use

less

Transcript and Presenter's Notes

Title: PHP Functions


1
PHP - Functions
  • We have looked at a few existing PHP functions
    count, define, include, array_pop, array_push,
  • We can also code user-defined functions

2
PHP User-defined Functions
  • function functionName( parameterList )
  • // any valid PHP code

3
PHP User-defined Functions
  • The default is pass by value
  • function foo( a )
  • // a is passed by value

4
PHP User-defined Functions
  • Can pass by reference add in front of the
    variable name
  • function foo( a )
  • // a is passed by reference

5
PHP Pass by reference
  • function add_some_extra( string )   string
    . world'str Hello 'add_some_extra(str
    )echo str    ? Hello world

6
PHP Default argument values
  • function makecoffee(type "cappuccino")  
    return Cup of type"echo makecoffee( ) ?
    Cup of cappuccinoecho makecoffee("espresso")
  • ? Cup of espresso

7
PHP Variable length argument list
  • PHP 4 and above has support for variable-length
    argument lists in user-defined functions.
  • Use the func_num_args( ), func_get_arg( ),
    func_get_args( ) functions to retrieve arguments,
    ....

8
PHP func_num_args
  • Returns the number of arguments passed to the
    function
  • function foo( )    numargs func_num_args(
    )   echo of arguments numargs\n"
    foo(17, 22, 38)    ? of arguments 3

9
PHP func_get_arg
  • Return an item from the argument list
  • function foo( )      num func_num_args(
    )     if (num gt 2)
  •      echo 2nd argument " ,
    func_get_arg(1)     foo (15, 22, 37) ?
    2nd argument 22

10
PHP func_get_args
  • Returns an array in which each element is the
    corresponding member of the current user-defined
    function's argument list
  • function foo( )    arg_list func_get_args(
    )   for (i 0 i lt count( arg_list) i)
  •        echo "Argument i is ,
    arg_listi, \n   foo(17, 22, 35)

11
PHP Value returning function
  • Values are returned by using the return
    statement. Any type may be returned, including
    lists and objects.
  • function square( num )    return num
    numecho square(4)  ? 16

12
PHP More built-in functions
  • phpinfo( )
  • ? info on php version,
Write a Comment
User Comments (0)
About PowerShow.com