JacobBar Posted January 12, 2010 Share Posted January 12, 2010 this is probably very easy but im a newbee and couldnt find a solution by googling it, so im trying to call a variable at the beginning of the code and the variable is at the end of the code like below <?php echo $test; $test =' this is a test'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/ Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 You cannot call a variable before it is defined, although you can use output buffering it is not recommended. Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993369 Share on other sites More sharing options...
The Little Guy Posted January 12, 2010 Share Posted January 12, 2010 you cant echo it then set it, you have to set it then echo it. <?php $test =' this is a test'; echo $test; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993371 Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 Yes, if you want to provide the reasoning behind why you're wanting to echo a variable like this, there's other solutions, but if you're learning, you've learned. Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993377 Share on other sites More sharing options...
JacobBar Posted January 12, 2010 Author Share Posted January 12, 2010 thanks for the info, the reason is im trying to make a menu that when the user select a page "about us" it would have a different background image by doing so <td width="94" <?php if ($page = "aboutus" ) echo "background=\"aboutus_selected.gif\""; else echo "background=\"aboutus_not_selected.gif\""; ?> ><a href="about.php"></a></td> $page = 'aboutus'; Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993385 Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 Why can't you define $page above that html except? It would be more syntatically correct and fix your problems. Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993386 Share on other sites More sharing options...
JacobBar Posted January 12, 2010 Author Share Posted January 12, 2010 i would sure do so if it was possible but the header and the menu is the same code thats included in the beginning of every page Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993389 Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 Then you'd have to include the $page variable before the included code. $page = 'aboutus'; include('aboutus.php'); echo $page; Aboutus.php is included, therefor the $page variable is passed into it. Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993392 Share on other sites More sharing options...
JacobBar Posted January 12, 2010 Author Share Posted January 12, 2010 not sure why i thought it wasnt possible to define a var before the included head/menu code maybe its time to get some sleep thanks oni-kun Quote Link to comment https://forums.phpfreaks.com/topic/188159-variable-help/#findComment-993394 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.