Inferno Web Development
  • Home
  • Forums
  • About
  • Links
  • Contact

Tutorials List

  • C++ Tutorials

Articles

  • Java
  • PHP
  • Photoshop
  • C#
  • C++ Qt GUI
  • C++ Win32 API
  • C++
  • MASM32
  • General News
  • JavaScript
  • Web Development
  • Windows Tweaks

Popular Tutorials

All time:

  • Website Header with 3D Fold Up / Lift Effect
  • C++ Win32 API Tutorial
  • Perfect C++ String Explode Split
  • Simple C++ DLL Loading a Message Box
  • Simple C++ Pointers and References

Related Articles

  • PHP IndexOf Key like JavaScript

Programming Tutorials

Home

Global Scope

Submitted by Baran Ornarli on Sat, 08/16/2008 - 13:23.
  • PHP

Table of Contents

  1. Static Variables
  2. Scope Operator ::

Scope can be a confusing concept for many beginners in PHP. The idea is that some variables and functions are invisible to each other in certain circumstances of code.

If you declare a variable outside of a function, let's say $C_array. The function cannot see it until you declare it as a global inside the function.

$C_array = array(5,2,1);
function Test(){
  global $C_array; // without this, C_array would be out of scope.
  $C_array[] = 7;
}

Alternatively you can use the superglobal $GLOBALS['C_array'].

Static Variables

Static variables can be useful for recursive functions.

Most variables declared inside a function are in the local scope and they stay there until the function finishes. However, if you restart the function recursively you may want a variable that doesn't just disappear when the function restarts.

function Recursive(){
  static $count = 0; // do not put an expression here.
  $count++;
  if($count < 25){
    Recursive();
  }
  count--;
}

Scope Operator ::

As long as you have a static function in the class, you can call a class's function without having to initiate the class. Just make sure you don't use a function that is pulling variables that were established in the constructor or the class that isn't static.


5
Average: 5 (1 vote)
»
  • Share Tutorial

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <img> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h2>
  • Lines and paragraphs break automatically.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Table of contents based on the <h*> tags

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
                         oooooooooooo                                      
`888' `8
.ooooo oo oo.ooooo. 888 oooo ooo oooo ooo .oooo.
d88' `888 888' `88b 888oooo8 `88. .8' `88. .8' `P )88b
888 888 888 888 888 " `88..8' `88..8' .oP"888
888 888 888 888 888 `888' `888' d8( 888
`V8bod888 888bod8P' o888o .8' .8' `Y888""8o
888. 888 .o..P' .o..P'
8P' o888o `Y8P' `Y8P'
"
Enter the code depicted in ASCII art style.

Navigation

  • Home
  • Forums
  • Image Gallery
  • Links
  • About
  • Contact

Why Register? Contribute articles and tutorials to ID, earn titles, learn from ID programming projects, and advertise your blog in our forums.

  • Forum Community
  • Register Now
  • Write an Article


Copyright © Inferno Development 2008-2009. All Rights Reserved.