Andy Rivers Posted May 5, 2016 Share Posted May 5, 2016 Hi all, I have the following PHP code block: <?php $website = "http://www.nationalgeographic.com"; echo 'Catch up on all the latest, visit: <a href=" ' . $website .' ">National Geographic</a>'; //works echo "<br><br>"; echo 'Catch up on all the latest, visit: <a href= ' . $website .' >National Geographic</a>'; //works echo "<br><br>"; echo 'Catch up on all the latest, visit: <a href=" . $website . ">National Geographic</a>'; // Does not work ?> Why do examples 1 and 2 only work and which is the best option to go with, if any, around the <a href="">? i.e. <a href=" ' . $website . ' "> or <a href= ' . $website . ' > or does it matter? Thanks, Andy ;-) Quote Link to comment Share on other sites More sharing options...
requinix Posted May 5, 2016 Share Posted May 5, 2016 Always use quotes with HTML attributes. But your two versions with quotes are also adding spaces that shouldn't be there: "......"is going to produce Quote Link to comment Share on other sites More sharing options...
Andy Rivers Posted May 5, 2016 Author Share Posted May 5, 2016 Ok thanks, I just put the spaces for ease of reading. I see now it is all to do with the declaring and initialising of the variables and if double or single quotes have been used in that part. Thanks, andy ;-) Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 5, 2016 Share Posted May 5, 2016 IMHO - it is usually easier to start one of these with double quotes and then wrap the attributes in single ones, that way your php vars are properly recognized. Quote Link to comment Share on other sites More sharing options...
maxxd Posted May 6, 2016 Share Posted May 6, 2016 A variable in double quotes will be parsed, whereas a variable in single quotes will not be parsed, but rather treated as a string literal - it doesn't matter where or how the variables are declared. 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.