Re: namespace trouble

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



Hi Pascal. You seem to have a problem with the scope of your variables.

On Thu, 31 May 2007, Pascal Desroche wrote:

To: Andy Stratton <astratton@xxxxxxxxxxxxxxx>
From: Pascal Desroche <pascal@xxxxxxx>
Subject: Re:   namespace trouble

Yes Anddy, i did this to get every kind of info i could. i just don't understand why this


<?php


//here you are declaring $example in the global scope.
$example = "php variable test";


PrintExample();

function PrintExample(){

// now you are trying to access a global variable
// inside a function
echo "$example";

}

?>

Notice: Undefined variable example

to do this you need to use the global keyword inside the function, like this:

function PrintExample() {

// tell php you want to access a global variable
global $example;

// or global $var1, $var2, ... etc

echo $example;

}

It's not good practice to access global variables like that anyway. When you get onto OOP PHP then that would be frowned upon.

Usually you would want to pass the global variable as a parameter to the function, like this:


PrintExample('testing...');

function PrintExample($example) {
  echo $example;
}

HTH

Keith

------------------------------------------------------------
http://www.karsites.net
http://www.raised-from-the-dead.org.uk

This email address is challenge-response protected with
http://www.tmda.net
------------------------------------------------------------

[Index of Archives]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [Postgresql]     [PHP Books]     [PHP Databases]     [PHP SOAP]
  Powered by Linux