Jump to content

Variable Scope Prob....help please!!


dayo

Recommended Posts

Some help please.  I am trying to modify some files that have the following simplified structure

<?php
include 'a.inc';
include 'b.inc';
?>

a.inc has the following struture
<?php
"A form for User Input"
include 'A1.inc';
include 'A2.inc';
?>

A1.inc has the following struture
<?php
function 1 {
$x = "Value from user input form"
}
?>

A2.inc has the following struture
<?php
function 2 {
$y = $x + 1
}
include 'A2_1.inc';
?>

A2_1.inc has the following struture
<table>
<tr>
<td>"The value of $y"</td>
</tr>
</table>


The files are a bit more complicated than this (Gallery2 photo display software) but this is the basic structure.  I am struggling to get "The value of $y".  Any tips anyone???

Thanks for your help
Link to comment
https://forums.phpfreaks.com/topic/23823-variable-scope-probhelp-please/
Share on other sites

If you want your variables to be shared between all your files and functions, you can put "global $y" at the top of each function that uses $y.  And global $x at the top of each that uses $x.

Another way is to pass the variable in as function arguments and get them back as return values.  Once your code gets larger you will probably want to do that.
[quote author=btherl link=topic=111365.msg451247#msg451247 date=1160720432]
If you want your variables to be shared between all your files and functions, you can put "global $y" at the top of each function that uses $y.  And global $x at the top of each that uses $x.

Another way is to pass the variable in as function arguments and get them back as return values.  Once your code gets larger you will probably want to do that.
[/quote]

Thanks.  Sorry but a follow on query.  Does it matter that $x & $y are actually arrays?  The code is indeed large...I have just simplified the structure.  Any clues on how I pass the variables across please?

If I will be brazen, can you (or anyone else) illustrate with my example how this can be done?

Thanks for the hints
For scope, it doesn't matter if they are arrays.

Here's an example for passing variables into functions and getting results back:

[code]function increment($x) {
  $x = $x + 1;
  return $x;
}

$x = 1; # This is NOT the same $x as above, it is outside the function scope
$x = increment($x);
print "x is $x\n";[/code]

There's no connection between the $x inside the function and the $x outside.  You can check this by doing

[code]$x = 1;
print "Incremented x is " . increment($x) . "\n";
print "But x's value is still $x\n";[/code]

Good luck :)
Thanks a lot.  In my example therefore I will have....
[code]
<?php
include 'a.inc';
include 'b.inc';
?>

a.inc will have the following
<?php
"A form for User Input"
include 'A1.inc';
include 'A2.inc';
?>

A1.inc will have the following
<?php
function 1 {
$x = "Value from user input form"
return $x;
}
?>

A2.inc will have the following
<?php
function 2($x) {
$y = $x + 1
return $y
}
include 'A2_1.inc';
?>

A2_1.inc will have the following
<table>
<tr>
<td>"The value of $y"</td>
</tr>
</table>
[/code]

Am I getting it?  Sorry if I seem a bit thick but because of the complexity of the code, I want to get it right at this level before diving in.

Thanks for the help

[quote]Sorry if I seem a bit thick but because of the complexity of the code, I want to get it right at this level before diving in.[/quote]

You best read the manual on the subject of [url=http://au.php.net/manual/en/language.variables.scope.php]scope[/url] then, this really is one of the most fundemental lessons that [b]must[/b] to be learnt.
[quote author=thorpe link=topic=111365.msg451277#msg451277 date=1160730730]
You best read the manual on the subject of [url=http://au.php.net/manual/en/language.variables.scope.php]scope[/url] then, this really is one of the most fundemental lessons that [b]must[/b] to be learnt.
[/quote]

I bet it is.  I already read the page you referenced even before signing up here yesterday.  As you are far more knowledgeable on the subject than I am, can you please indicate if i am on the right track with the interpretation I gave??

Thanks a lot.

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.