dayo Posted October 13, 2006 Share Posted October 13, 2006 Some help please. I am trying to modify some files that have the following simplified structure<?phpinclude '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<?phpfunction 1 {$x = "Value from user input form"}?>A2.inc has the following struture<?phpfunction 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 Quote Link to comment Share on other sites More sharing options...
btherl Posted October 13, 2006 Share Posted October 13, 2006 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 Link to comment Share on other sites More sharing options...
dayo Posted October 13, 2006 Author Share Posted October 13, 2006 [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 Quote Link to comment Share on other sites More sharing options...
btherl Posted October 13, 2006 Share Posted October 13, 2006 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 :) Quote Link to comment Share on other sites More sharing options...
dayo Posted October 13, 2006 Author Share Posted October 13, 2006 Thanks a lot. In my example therefore I will have....[code]<?phpinclude '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<?phpfunction 1 {$x = "Value from user input form"return $x;}?>A2.inc will have the following<?phpfunction 2($x) {$y = $x + 1return $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 Link to comment Share on other sites More sharing options...
trq Posted October 13, 2006 Share Posted October 13, 2006 [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 Link to comment Share on other sites More sharing options...
dayo Posted October 13, 2006 Author Share Posted October 13, 2006 [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. Quote Link to comment Share on other sites More sharing options...
dayo Posted October 14, 2006 Author Share Posted October 14, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.