User Defined Functions in PHP - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

User Defined Functions in PHP

Description:

User Defined Functions in PHP. why do you need functions ... var = say_hi(); print $var; print say_hi(); User Defined Functions in PHP. Function Arguments ... – PowerPoint PPT presentation

Number of Views:523
Avg rating:3.0/5.0
Slides: 13
Provided by: djo4
Category:
Tags: php | defined | functions | user | var

less

Transcript and Presenter's Notes

Title: User Defined Functions in PHP


1
User Defined Functions in PHP
  • why do you need functions/subroutines?
  • modular codes, nice programming practice
  • pass parameters and get values
  • avoid repeating certain lines again and again

2
User Defined Functions in PHP
  • lt?php function say_hi() print "Hi
    there!"
  • say_hi()
  • echo I will say Hi again
  • say_hi()
  • ?gt

3
User Defined Functions in PHP
  • Returning values from a function
  • lt?php function say_hi() return "Hi
    there!"var say_hi() print var
    print say_hi()?gt

4
User Defined Functions in PHP
  • Function Arguments
  • lt?php function say_hi(name) return "Hi
    there name!"
  • print say_hi("Billy Joe Bob")
  • ?gt

5
User Defined Functions in PHP
  • lt?php function say_hi(name, greeting)   
     return "greeting name!"print
    say_hi("Billy Joe Bob", "Hi there") ?gt

6
User Defined Functions in PHP
  • Scope of variables?
  • where in a script a variable can be used
  • variables are available in whole script except
    functions
  • you have to pass parameters to functions
  • variables inside functions cant be used outside

7
Global and Local variables
  • Global Variables
  • Declared/defined in the main script
  • Have a larger scope
  • Local Variables
  • Declared/defined in functions
  • Have a smaller scope

8
User Defined Functions in PHP
  • lt?php x 1function add()   x10   
  • xadd()print x?gt

9
User Defined Functions in PHP
  • lt?php x 1function add()
       GLOBALS"x"add() print x?gt

10
User Defined Functions in PHP
  • lt?php
  • link mysql_connect("localhost", "ist210",
    "ist210")
  • db "ist210" table_name "gymnasts"
  • mysql_select_db(db, link)
  • result mysql_query("select from
    gymnasts")
  • display_records(result)
  • mysql_close(link)
  • function display_records(records)
  • while(one_record mysql_fetch_row(records
    ))
  • foreach (one_record as field)
  • print "field "
  • print "ltbrgt"
  • ?gt

11
User Defined Functions in PHP
  • function display_records(records)
  • print "lttable width200 border1gt\n"
  • while(one_record mysql_fetch_row(records)
    )
  • print lttrgt\n
  • foreach (one_record as field)
  • print "\tlttdgtfieldlt/tdgt\n"
  • print "lttrgt\n"
  • print "lt/tablegt\n"

12
Including files
  • lt?php
  • //Global variables
  • include("filename.php")
  • //here comes the code for this file
  • ?gt
  • Global variables can be used inside included
    files!!!!!
Write a Comment
User Comments (0)
About PowerShow.com