Page 1 of 1

Global Variables and include statement

Posted: Wed Aug 25, 2010 5:34 pm
by devarishi
Hi,

When we include a PHP script inside a PHP file the variables declared in the included PHP script are accessible in the PHP file that includes that PHP script.

Those variables can be assigned a different value in the calling PHP script. How to prevent it?

Please, see these two example PHP files:

global.php

Code: Select all

<?php

	$currency	=	"Rs.";
	$line		=	str_repeat("_",80);

?>
----------------------------------------------------------

include.php

Code: Select all

<?php


	include("global.php");


	echo $currency;
	echo $line;

	$line = "NO LINE";

	echo $line;

?>
Output:

Code: Select all

Rs.________________________________________________________________________________NO LINE



The variable $line is being assigned a new value in the second PHP file. How to prevent it?

Re: Global Variables and include statement

Posted: Wed Aug 25, 2010 7:38 pm
by JakeJ
Your solution is to use a different variable name.

Because you call global.php first, you're going to see the output of what is in global.php before you change the value of the variable.

The scope of $line is maintained exactly as it's supposed to be. So the only way to keep them running over the top of each other is to use different variable names.

Re: Global Variables and include statement

Posted: Thu Aug 26, 2010 3:09 am
by Mordred
PHP is a bit (okay, A LOT) clumsy in dealing with variable scope, that's why people use things like registries to avoid deaing with such scoping issues.

Re: Global Variables and include statement

Posted: Thu Aug 26, 2010 3:50 am
by amargharat
<?php


include("global.php");


echo $currency;
echo $line;

$line = "NO LINE";

echo $line;

?>
Above your code is exhibiting correct output, bcos,
1) you are echoing previous values of $currency and $line.
2) Then you are assigning new value to $line

just comment that echo $currency and $line. and put only last echo statement.
then you will see recent assigned value of $line.

It means $line variable get assigned by new value. and echoing also new value.

Things is that you are misunderstanding.

Re: Global Variables and include statement

Posted: Thu Aug 26, 2010 6:45 am
by devarishi
amargharat wrote:
Things is that you are misunderstanding.

Amar, my point is that the variable $line is declared in global.php and I want to protect it from being assigned a new value elsewhere wherever the file global.php is being included. Or in other words, can we protect variables from being assigned values in another script where they are being used?

Or yet in other words, I want to throw an error when a variable ($line in our example) is assigned a new value in a script (include.php) other than the one wherein it is originally declared and assigned initial values or changed anywhere in that script (global.php).

Re: Global Variables and include statement

Posted: Thu Aug 26, 2010 7:40 am
by JakeJ
Well now you're talking about a constant rather than a variable. A variable, by definition is changeable.

Check the php manual for define(). http://php.net/manual/en/function.define.php

Re: Global Variables and include statement

Posted: Thu Aug 26, 2010 10:28 am
by devarishi
JakeJ wrote:Well now you're talking about a constant rather than a variable. A variable, by definition is changeable.

Check the php manual for define(). http://php.net/manual/en/function.define.php

Constants can't assigned new values even in the same script once they are declared and defined.

I think the variable $line in the given example is working as a local variable in the file include.php. I will try including global.php again after assigning a new value to $line, and see if the seond time "echo $line" prints a line or it prints "NO LINE".

So, I will try this:

Code: Select all

<?php
include("global.php");

echo $currency;
echo $line;

$line = "NO LINE";
echo $line;

include("global.php");
echo $line;
?>

Re: Global Variables and include statement

Posted: Thu Aug 26, 2010 10:40 am
by JakeJ
If global.php is included in your other file before anything else, it's going to echo the value of $line.

Now, if you define $line in global.php but don't echo it, you can change the value THEN echo it out and it will reflect the new value.

Maybe it would be helpful if you want and read some material on variable scope.

Re: Global Variables and include statement

Posted: Sun Sep 12, 2010 6:38 am
by kaisellgren
Hint: PHP 5.3+ implements namespaces.

Re: Global Variables and include statement

Posted: Mon Sep 13, 2010 6:21 pm
by devarishi
kaisellgren wrote:Hint: PHP 5.3+ implements namespaces.

Namespace... that sounds PHP OOPS... I will check it out. Thanks for the hint! :mrgreen: