Jump to content


Photo

Variable Scope Prob....help please!!


  • Please log in to reply
7 replies to this topic

#1 dayo

dayo
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 October 2006 - 05:58 AM

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

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 13 October 2006 - 06:20 AM

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.

#3 dayo

dayo
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 October 2006 - 07:44 AM

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.


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

#4 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 13 October 2006 - 08:20 AM

For scope, it doesn't matter if they are arrays.

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

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";

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

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

Good luck :)

#5 dayo

dayo
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 October 2006 - 09:03 AM

Thanks a lot.  In my example therefore I will have....
<?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>

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



#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 13 October 2006 - 09:12 AM

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.


You best read the manual on the subject of scope then, this really is one of the most fundemental lessons that must to be learnt.

#7 dayo

dayo
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 October 2006 - 09:21 AM

You best read the manual on the subject of scope then, this really is one of the most fundemental lessons that must to be learnt.


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.

#8 dayo

dayo
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 14 October 2006 - 06:33 PM

Managed to find a way to solve the main issue I faced.  Still have some work to do but want to thank contributors for their help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users