/    Sign up×
Community /Pin to ProfileBookmark

simple php variable question – variable definitions being lost

I am sure there is a simple solution to this, but I have found that when I define a variable in one place that it isn’t always available within the rest of the page. For example:

[CODE]
<?php include(“includes/header.php”); //within header $myVariable is declared (by $myVariable = 2;) ?>

<html>
<head>
</head>
<body>
<p>
my variable = <?php echo $myVariable ?>
</p>

</body>
</html>
[/CODE]

Is there a way to declare a variable inside an include and have it remembered by the rest of the page that the include is inserted into?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@tirnaFeb 02.2011 — [code=php]
<?php echo $myVariable ?>

[/code]


If nothing is being outputed in the above line then either the variable is not being properly declared in the include file or you have incorrectly set the path to the included file in your include statement.

To first check if the included file is being found add

[code=php]
echo 'got here'; die();
[/code]
in the first line of header.php and see if the text string is displayed in your browser when the include statement is executed. If 'got here' doesn't appear then the path to header.php is not correct.
Copy linkTweet thisAlerts:
@NogDogFeb 02.2011 — It might also be a scope issue if either the variable is declared within a function definition, in which case it is only local to that function.

PS: you could use require() instead of include() to find out if the file is really being included or not.
Copy linkTweet thisAlerts:
@BluegillMediaauthorFeb 02.2011 — I double checked my code.

1) the include file is linked correctly

2) the include file is declaring the variable correctly (I can echo it from within the include file)

I did notice that my code on the page is trying to echo the variable (that is declared in the include file) from within a function.

Can a function not see variables that are declared outside the function?
Copy linkTweet thisAlerts:
@tirnaFeb 02.2011 — 
Can a function not see variables that are declared outside the function?[/quote]


Ok, then you have a variable scope issue.

Have a read through the php manual on variable scope.
Copy linkTweet thisAlerts:
@BluegillMediaauthorFeb 02.2011 — So if I have this right, I just have to put [CODE]global $myVariable;[/CODE] above the declaration and I should be good to use it anywhere?
Copy linkTweet thisAlerts:
@tirnaFeb 02.2011 — So if I have this right, I just have to put global $myVariable; above the declaration and I should be good to use it anywhere?[/quote]

What happened when you tried it?
Copy linkTweet thisAlerts:
@NogDogFeb 03.2011 — [b]global[/b] may be a quick fix, but it's generally a non-optimal solution, as it closely couples the function to the rest of the application, which can lead to ugly bugs and limit re-usability. Without a good feel for what all is going on in this case, I'm not sure what the better solution is, but generally you want to feed variables [i]into[/i] a function via the function's arguments (a.k.a. "parameters") and get them [i]out[/i] of the function either by [b]return[/b]ing them, or via a "pass by reference" function parameter.
Copy linkTweet thisAlerts:
@BluegillMediaauthorFeb 08.2011 — It didn't work when i tried it. I will double-check it and see if I had an error. Likely I will end up breaking down and passing the variable with the function call (which is what I was trying to avoid since I have to call the function about 20 times).
Copy linkTweet thisAlerts:
@NogDogFeb 08.2011 — It didn't work when i tried it. I will double-check it and see if I had an error. Likely I will end up breaking down and passing the variable with the function call (which is what I was trying to avoid since I have to call the function about 20 times).[/QUOTE]

But that's just typing: you can type a variable name 20 times in a minute or two if you're a decent typist (or less with a good editor/IDE with auto-completion), which beats the heck out of debugging for hours/days to avoid it, while also avoiding the use of global variables which are frowned upon anyway. ?
Copy linkTweet thisAlerts:
@BluegillMediaauthorFeb 08.2011 — true enough...
Copy linkTweet thisAlerts:
@BluegillMediaauthorFeb 17.2011 — I did try the global variable, but haven't got it to work. May play around with it later, if it is needed.
×

Success!

Help @BluegillMedia spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...