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! Link to comment https://forums.phpfreaks.com/topic/204207-variable-scope-mixing-php-and-html/ 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 Link to comment https://forums.phpfreaks.com/topic/204207-variable-scope-mixing-php-and-html/#findComment-1069588 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. Link to comment https://forums.phpfreaks.com/topic/204207-variable-scope-mixing-php-and-html/#findComment-1069634 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 ? Link to comment https://forums.phpfreaks.com/topic/204207-variable-scope-mixing-php-and-html/#findComment-1069638 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> Link to comment https://forums.phpfreaks.com/topic/204207-variable-scope-mixing-php-and-html/#findComment-1069674 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 Link to comment https://forums.phpfreaks.com/topic/204207-variable-scope-mixing-php-and-html/#findComment-1069680 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 Link to comment https://forums.phpfreaks.com/topic/204207-variable-scope-mixing-php-and-html/#findComment-1069724 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.