Jump to content

[SOLVED] really quick variable scope question


Lodius2000

Recommended Posts

to call a function defined in 1 function within the definition of another function you need a global line right

 

ie

<?php

function blah(){
$a = "howdy world";
}

function bleh(){
global $a;

//prints howdy world
print $a;
}
?>

 

sorry dumb question I know but its been a while for this sort of thing

 

Thanks

<?php
function blah(){
$a = "howdy world";
}

function bleh(){
global $a;

//prints howdy world
print $a;
}
bleh();

will output nothing as $a is local to blah and not global to the whole script

<?php
function blah(){
global $a;
$a = "howdy world";
}

function bleh(){
global $a;

//prints howdy world
print $a;
}
bleh();

will still not work because the value of $a is never set even though it is global in both in functions

but:

<?php
function blah(){
global $a;
$a = "howdy world";
}

function bleh(){
global $a;

//prints howdy world
print $a;
}
blah();
bleh();

will output "howdy world" because the global variable $a is set in blah() then read in bleh()

 

Scott.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.