Home > Archive > PHP Installation > May 2007 > Re: [PHP-INSTALL] namespace trouble
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: [PHP-INSTALL] namespace trouble
|
|
| Keith Roberts 2007-05-31, 7:01 pm |
| 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@compassinfo.net>
> From: Pascal Desroche <pascal@ker.org>
> Subject: Re: [PHP-INSTALL] 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
------------------------------------------------------------
| |
| Pascal Desroche 2007-05-31, 7:01 pm |
| thanx a lot Keith ;
Keith Roberts a =E9crit :
> Hi Pascal. You seem to have a problem with the scope of your=20
> variables.
>=20
> On Thu, 31 May 2007, Pascal Desroche wrote:
>=20
>=20
> <?php
>=20
>=20
> //here you are declaring $example in the global scope.
> $example =3D "php variable test";
>=20
>=20
> PrintExample();
>=20
> function PrintExample(){
>=20
> // now you are trying to access a global variable
> // inside a function
> echo "$example";
>=20
> }
>=20
> ?>
>=20
>=20
> to do this you need to use the global keyword inside the=20
> function, like this:
>=20
> function PrintExample() {
>=20
> // tell php you want to access a global variable
> global $example;
>=20
> // or global $var1, $var2, ... etc
>=20
> echo $example;
>=20
> }
>=20
> It's not good practice to access global variables like that=20
> anyway. When you get onto OOP PHP then that would be=20
> frowned upon.
>=20
> Usually you would want to pass the global variable as a=20
> parameter to the function, like this:
>=20
>=20
> PrintExample('testing...');
>=20
> function PrintExample($example) {
> echo $example;
> }
>=20
> HTH
>=20
> Keith
>=20
> ------------------------------------------------------------
> http://www.karsites.net
> http://www.raised-from-the-dead.org.uk
>=20
> This email address is challenge-response protected with
> http://www.tmda.net
> ------------------------------------------------------------
>=20
|
|
|
|
|