Home » Code, PHP Functions, Tutorials

PHP Global Variables with Functions

Here we’re going to take a look at variables inside and outside of PHP functions. It is very easy to make one error with a function and have it return the wrong value. We’re going to make a simple function named “func1” which we’ll tweak a bit to show how variables are used inside and out of functions in PHP.

Here is our function, take a look and see if you can tell what the output of the function will be:

<?php

function func1(){

}

$var = func1();
echo $var;

?>

The output is nothing right now. haha So if you thought it was anything but nothing, that’s wrong. The function has no code in it whatsoever right now, and the variable “$var” is set to equal the returned value of the function “func1().” Since the function does nothing, $var is equal to nothing. Easy enough. Now let’s work with some variables.

<?php

function func1($var){
return $var; // returning the exact value of $var.
}

$var = func1("hello"); // $var is equal to the function func1() with parameter "hello"
echo $var;

?>

Now, we have a parameter to our PHP function. A parameter is any variable that is defined within the function’s parentheses. You can have many parameters in your functions – seperated by commas – but it is  a good idea to keep your functions organized and simple to their purpose. So we have $var as a parameter to func1(). You can use these to submit values through a function and work with them inside the function to return a different or desired value. Right now, the function func1() is just returning $var. So whatever is put through as $var initially, will be returned back as it was.

So this code will result in the output:

hello

Let’s say we want to have a global variable we can use in several functions. Global variables are defined as such within a function as previously defined variables outside the function. In the following code we’ll define $name as “Brian” and use it in two functions.

<?php

$name = "Brian";

function hello(){
global $name;
return "Hello ".$name."!";
}

function goodbye(){
global $name;
return "Goodbye ".$name."!";
}

$hello = hello();
$goodbye = goodbye();

echo $hello;
echo "<br/>";
echo $goodbye;

?>

The output of this code will be:

Hello Brian!
Goodbye Brian!

You can use global variables with PHP pages you include on other PHP pages. For example if you have variables defined on one PHP page, in this example “variables.php,” you can call that page to another and globally define variables in your functions to work with them and return what you like.

Variables.php:

<?php

$name = "Brian";
$website = "bgallz.org";

?>

Now on our page we are working on, we’ll call it “display.php,” we’ll call the page variables.php and use these variables in functions.

Display.php:

<?php

include("variables.php") or die("Could not find variables.php");

function hello(){
global $name;
global $website;

return "Hello ".$name."! Thanks for visiting ".$website."";

}

$var = hello();
echo $var;

?>

The output of this code will be:

Hello Brian! Thanks for visiting bgallz.org.

0

Popularity: 1% [?]

Share/Bookmark this!

One Comment

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on May 27, 2010 at 3:59 PM and filed under Code, PHP Functions, Tutorials category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors