rramsey Posted June 8, 2010 Share Posted June 8, 2010 Hi, I know all the ways to work around this, but now I'm just curious. On php 4.3.x I want to mix blocks of php code and html code and have the variables from one php block accessible in other php blocks. For example: <?php global $test; $test = "hello world"; ?> <html> <body> <p> <?php print "test is: $test -"; ?> </p> <p> <?php global $test; print "test is: $test -"; ?> </p> </body> </html> Both paragraph blocks print the same thing, "test is: -" with $test having no value. Is there a way to do this? Searching brings back so many responses, all about variable scope and using global, that I couldn't find anything. There are about 10 different ways to solve the problem, but I just got curious if this was even possible. Thanks! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 8, 2010 Share Posted June 8, 2010 The global command only works within functions. You have to learn about variable scope Ken Quote Link to comment Share on other sites More sharing options...
rramsey Posted June 8, 2010 Author Share Posted June 8, 2010 Yep, I knew that. I was experimenting to see if the global function would work in the different blocks. I didn't see anything on the manual page or in the notes about working between the <?php ?> blocks like it does for functions. I figured if I left it in, people wouldn't suggest global. Session variables would work as well, I guess. Quote Link to comment Share on other sites More sharing options...
xcandiottix Posted June 8, 2010 Share Posted June 8, 2010 i don't know if this would work but what if you did an include() with the variables on the included page? So if you included variables.php and it had $test = 'hello world' then wouldn't each paragraph print test is: hello world ? Quote Link to comment Share on other sites More sharing options...
ZachMEdwards Posted June 8, 2010 Share Posted June 8, 2010 <?PHP $test = 'test variable'; ?> <html> <body> <p> <?PHP echo '$test = '.$test; ?> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 8, 2010 Share Posted June 8, 2010 When PHP is processing your script, anything outside the <?php ?> tags not processed, so something like <?php // // php stuff // $var = 'stuff'; ?> <html stuff> <?php // // more php stuff // echo $var; ?> looks like <?php // // php stuff // $var = 'stuff'; // // more php stuff // echo $var ?> to the PHP Processor. There are no "blocks". It's all one script. Ken Quote Link to comment Share on other sites More sharing options...
Zagga Posted June 9, 2010 Share Posted June 9, 2010 In your particular case, it's not about the scope of variables, rather the way you are trying to display them. Your best approach is . . . <?PHP $test = 'test variable'; ?> <html> <body> <p> <?PHP echo '$test = '.$test; ?> </p> </body> </html> Zagga 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.