logo
g Text Version
Auto
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Society & Culture
Sports
Travel & Leisure
TV & Movies

dailyclick
Bored? Games!
Postcards
Astrology
Take a Quiz
Rate My Photo

new
English Garden
Costuming
Charity
Women's Fashion
Pop Music


dailyclick
All times in EST

Tatting: 13:00 PM

Full Schedule
g
g PHP Site
Editor Wanted
BellaOnline's PHP Editor

g

The PHP Global Statement
Guest Author - Diane Cipollo

The global statement brings an outside variable "into" a function. In our previous tutorial, you learned that a variable created outside of a PHP function is not available to be used by that function. Usually you will get around this problem by passing an argument when you call the function.

function print_name($first_name)
{echo "Hello $first_name";}

print_name("Susan Ann");


Another way to make an "outside" variable available to a function is to use the PHP global statement. The global statement brings the outside variable "into" the function. Let's take a look at the code.

$first_name = "Susan Ann";

function print_name( )
{global $first_name;
echo "Hello $first_name";}

print_name( );


$first_name = "Susan Ann";
This code will set the value of the $first_name variable to Susan Ann. This is not part of the function code.

function print_name( )
function function_name( )
This tells the program that the following is a PHP function. print_name is the name of the function and is used to ID the function to the program. Although we are not passing any arguments to the function, we still need to follow the function name with parenthesis.

Inside the opening { and closing } is the code for the body of the function. . .

global $first_name;
global $variable;
This is the global statement that will make the value to the $first_name variable available to the function. As you can see, this statement ends with a semicolon.

echo "Hello $first_name";
This line of code is the echo statement that will print Hello Susan Ann to the web browser.

print_name( );
This is the function call for the print_name function. As you can see, there is nothing inside the parenthesis. We do not need to pass an argument to the function because we are using the global statement.





This site needs an editor - click to learn more!

RSS | Previous Features | Site Map


Content copyright © 2008 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

Digg! g delicious Save to Del.icio.us

g


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor

g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Driving Amount
Much more
Slightly more
Slightly less
Much less

g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2008 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor