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

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

new
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


dailyclick
All times in EST

Low Carb: 8:00 PM

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

The Availability Of A PHP Variable

Guest Author - Diane Cipollo

Many beginning PHP programmers are confused as to when a PHP variable is available within a PHP program. Basically it depends on how and where the variable is declared or created. In previous tutorials*, you learned that in most cases you will declare a variable by giving it an initial value.

$dollar_amt = 10;

That takes care of how to declare a variable but where you declare a variable is also important. A variable is available only where it was created. For example, if we create a variable within a function, that variable is only available within that function. It does not exist outside of that function. Think of the function as a box that contains the variable and you cannot use the variable outside of that box. Let's take a look at an example. The function below creates the variable $dollar_amt and then prints a sentence to the web browser.

function print_cost()
{ $dollar_amt = 10;
echo "The cost is $dollar_amt dollars."; }

There is no problem here because we are using the variable in an echo statement and that echo statement is part of the same function that created the variable. (All of it is within the box.) But if we want to use the variable in a second echo statement later in the program, we will no longer be working inside the box. We will be attempting to use the variable "outside" of the original function that created it.

function print_cost()
{ $dollar_amt = 10;
echo "The cost is $dollar_amt dollars."; }
------
------
echo "You will need to send a check for $dollar_amt dollars.";

Because the variable is not available to the second echo statement the webpage will look like this.

You will need to send a check for dollars.

But you can create variables anywhere in your program and not just inside functions. What about a variable declared outside of a function, for example within the main body of your program? The same availability rule is true. The variable is available only where it was created (in this case outside the box). If you attempt to use the variable within a function/box, it will not be available. For example if you set the initial value of the $cent_amt variable to 50 in your program, you cannot use it inside the function below.

$cent_amt = 50;


function print_cost()
{ $dollar_amt = 10;
echo "The cost is $dollar_amt dollars and $cent_amt cents."; }

In the web browser you will only see the following sentence.

The cost is 10 dollars and cents.

But what about those times when you need to use an "outside" variable in a function? An easy way to solve this problem is to pass that variable as an argument when you call the function.

print_cost("50");

-------
*You may wish to read some of the previous tutorials about PHP variables and functions.




This site needs an editor - click to learn more!

Add The+Availability+Of+A+PHP+Variable to Twitter Add The+Availability+Of+A+PHP+Variable to Facebook Add The+Availability+Of+A+PHP+Variable to MySpace Add The+Availability+Of+A+PHP+Variable to Del.icio.us Digg The+Availability+Of+A+PHP+Variable Add The+Availability+Of+A+PHP+Variable to Yahoo My Web Add The+Availability+Of+A+PHP+Variable to Google Bookmarks Add The+Availability+Of+A+PHP+Variable to Stumbleupon Add The+Availability+Of+A+PHP+Variable to Reddit




RSS | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map


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


Content copyright © 2014 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.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor