words what do you read, m'lord?

 

« | main | »

includes and functions

includes

  • the usefulness of reusing code should be obvious to everyone!
  • require() and include() call code from a file
  • the main difference is that if something goes wrong, require() will return a fatal error, include() will only give a warning
  • data need not be passed to the include because it is inserted in the code and executed as if present all along (I think)
  • it’s a good idea to store include files outside the document tree, making them invisible to internet interlopers

functions

  • “A function is a self-contained module of code that prescribes a calling interface, performs some task, and optionally returns a result”
  • a library of user-defined functions can be stored in an include file
  • data must be passed to a function using a parameter (see “scope”, below)
  • user-defined functions must be declared, e.g.
  •   function say_hi()
      {
      echo 'hi';
      }

  • function names can only contain letters, digits, underscores; cannot begin with a digit; and cannot be the same as an existing function name

scope

  • scope controls where a variable is visible and useful
  • variables declared inside a function are in scope from the declaration until the function’s end brace; this is function scope, the variables are local variables
  • variables declared outside functions are in scope from the declaration to the end of the file, but not inside functions; this is global scope, the variables are global variables
  • if a require() or include() statement is used within a function, function scope applies; if not, global
  • the keyword global can be used to give a variable defined within a function global scope
  • to allow functions to operate on global variables, use a pass by reference; the function receives a reference to the original variable, and any modifications performed on the reference are also performed on the original
  • a pass by reference is created by prefixing an ampersand to the variable name in the function’s parameter list
  • the return keyword can be used to stop a function, usually in response to some condition; return can also be used to return a value
  • it is possible to call a function recursively, but this seems dangerous and doesn’t appear to have many benefits

Welling, Luke and Laura Thomson. PHP and MySQL Web Development. 2nd ed. Indianapolis: Sam’s, 2003.

Posted by pzed on January 24, 2008 at 4.55pm
Categories: libraries, music, web stuff

Post a comment