djmike Posted September 3, 2006 Share Posted September 3, 2006 I'm writing a script that will randomly insert news into a sidebox on my website. However, the site allows users to choose their 'skin' and this is passed to other pages via the url. So a link to a page called "hr0.php" is this: php:<a href="hr0.php?p=<? echo $p; ?>">Introduction</a>I have the code to randomly pick a number, then the HTML code to display that story and any assossciated images is loaded into a variable. This is all done in a separate file from the page where it will be displayed (as it will be used to fill the sideboxes of many pages.) In the sidebox I also want to include a link, so if the user is interested they can click it and be taken straight to the page. However, I am having trouble because it doesn't work having php code, within html code, being loaded into a php variable and then echoed. This is the code within my random picking file that loads the variable that I want to echo on my target page: php: $body = "<p align=\"center\"><img src=\"images/l2-019.jpg\" width=\"155\" height=\"156\" /></p> <p align=\"center\"> <a href=\"hr0.php?p=<? echo $p; ?>\">Introduction</a>"This works and does show the image and word "introduction" but upon clicking it the colour reverts to the default.Any ideas please?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/19565-how-to-store-php-and-html-code-to-a-variable/ Share on other sites More sharing options...
wildteen88 Posted September 3, 2006 Share Posted September 3, 2006 Just use:[code=php:0]$body = '<p align="center"><img src="images/l2-019.jpg" width="155" height="156" /></p> <p align="center"> <a href="hr0.php?p=' . $p . '">Introduction</a>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19565-how-to-store-php-and-html-code-to-a-variable/#findComment-85101 Share on other sites More sharing options...
djmike Posted September 3, 2006 Author Share Posted September 3, 2006 Thanks, that's sorted now. When should ' be used, and when should " be used, jsut for future reference Quote Link to comment https://forums.phpfreaks.com/topic/19565-how-to-store-php-and-html-code-to-a-variable/#findComment-85122 Share on other sites More sharing options...
ataria Posted September 3, 2006 Share Posted September 3, 2006 Aren't they the same thing -- jus that ' doesn't require the \ ? Quote Link to comment https://forums.phpfreaks.com/topic/19565-how-to-store-php-and-html-code-to-a-variable/#findComment-85123 Share on other sites More sharing options...
wildteen88 Posted September 3, 2006 Share Posted September 3, 2006 They are the samething yes. However if you ahve variables or whitespace characters (\n, \t, \r, etc) in single quotes PHP will just treat them as normal text. However if you used double quotes PHP wont treat them as normal text and will parse the variables and treat \n, \t, \n etc as whitespace characters.Also I use single quotes on html blocks to save having to type the backslash . This also helps keep the code a readable as possible. Quote Link to comment https://forums.phpfreaks.com/topic/19565-how-to-store-php-and-html-code-to-a-variable/#findComment-85201 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.