Archived Boards and Threads... > Parham's PHP Tutorials

PHP Lesson 13 - creating custom functions (one of many examples to come)

<< < (2/2)

adam:
Also, variables that get created in functions can only stay in functions.  You can print out a variable from a function outside the function.

[Unknown]:

--- Quote from: adam on October 17, 2004, 09:00:50 PM ---Also, variables that get created in functions can only stay in functions.  You can print out a variable from a function outside the function.

--- End quote ---

This is called variable scope.  That is, where the variable is "important".  There are two pertinent kinds of scope:

  - local scope: this is the scope inside functions, etc.  It means "local" to that function.
  - global scope: this is outside functions.  Things can be "extracted" from the global scope into the local scope.

Additionally, there are things called "superglobals" these are variables that are so important that they are in every scope.

To extract a variable from the global scope, use the global construct:

global $xyz;

From then on, if you change $xyz, the global variable $xyz will also be changed.  You can also use the superglobal $GLOBALS to access the global scope from any scope - in this case, you might use:

$GLOBALS['xyz'] = 4;

In this case, $GLOBALS is simply an associative array of all the variables in the global scope.... not so bad, eh?

-[Unknown]

adam:
Yeah unknown I just figured that out after reading the tut about it here.   :)  This is another thing I gotta build upon.  I have never created a function.

Navigation

[0] Message Index

[*] Previous page

Go to full version