spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Introduction to PHP Programming

By PHP Catalyst
2007-11-19


Scope of a Variable

By default, most variables in PHP will have only single scope. This single scope is available to the included files as well. For example

<?php
$a 
10;
$b 20;
$c 30;
include 
'cal.php';
?>

In the above example, the variables $a, $b, $c will be available within the included file cal.php as well. But this differs for the variables used inside a function. Let's see this example:

<?php 
$a 
10;
$b 20;
$c 30;

function 
print_sum() {
    
$d $a+$b+$c;
    echo 
$d;
  }
print_sum(); //calling the function  
?>

Upon execution of the above code, you will see 'Undefined Variable' notices. This is because, when you use $a, $b and $c variables within the function, they are treated as local variables and hence the value of which is not obtained from the variables used outside of the function. To be able to use these variables, you will have to declare them as global variables inside the function using the global keyword. See the modified example:

<?php 
$a 
10;
$b 20;
$c 30;

function 
print_sum() {
    global 
$a$b$c ;  //Declaring the variables as global variables
    
$d $a+$b+$c;
    echo 
$d;
  }
print_sum(); //calling the function  
?>

Now execute the code and you will see an output. This is because after declaring the variables as global, the variables which are declared outside the function becomes available within the function as well. Note that in our example, the variable $d is still a local variable within the function print_sum().

We hope that the above explanation was clear.

Tutorial Pages:
» What can I do with PHP?
» Popoular Features of PHP
» Basics of PHP
» Variables in PHP
» Data Types in PHP
» Expressions and Operators
» Control Structures in PHP
» Functions in PHP
» Declaring Functions in PHP
» Scope of a Variable
» Built-in Functions in PHP
» Handling Strings in PHP
» Printing Strings in PHP
» String Comparisons in PHP
» Manipulating Strings in PHP
» Arrays in PHP
» Types of Arrays in PHP
» Creating Arrays in PHP
» Array Operations in PHP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: