jwk811 Posted November 25, 2006 Share Posted November 25, 2006 i need to make a variable which is going to consist of html and the single quotes in there keep messing it up. is there a way i can make the single quotes still be apart of the variable but ignore it.. understand what im trying to do? thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/ Share on other sites More sharing options...
alpine Posted November 25, 2006 Share Posted November 25, 2006 Try to escape the single quotes if you are using single quotes around your variable definition[code]<?php$var = 'it\'s like this';$var = "it's like this";$var = <<<_HTMLit's like this_HTML;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129936 Share on other sites More sharing options...
jwk811 Posted November 25, 2006 Author Share Posted November 25, 2006 using the backslash will fix this? Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129942 Share on other sites More sharing options...
alpine Posted November 25, 2006 Share Posted November 25, 2006 yes, but if your variable contains a lot of html with single quotes - don't escape them all, use the latest example instad. With that you can mix both single and double quotes without any escaping. Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129943 Share on other sites More sharing options...
.josh Posted November 25, 2006 Share Posted November 25, 2006 yes. if you do $blah = 'moreblah'; // with single quotesand try to throw in a quote in there, php will think you are closing the string. the backslash tells php to treat it as a regular character instead of a special character (something it looks for to do something...like close the string. if you do $blah = "moreblah"; // with double quotesyou can use the single quote inside there, because php will not be looking for the single quote to close out the string. Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129944 Share on other sites More sharing options...
jwk811 Posted November 25, 2006 Author Share Posted November 25, 2006 oh i see but in the html there are also double quotes so that wouldnt work either.. thats actually how i had it started.. and alpine i dont understand the last var you have listed.. Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129958 Share on other sites More sharing options...
.josh Posted November 25, 2006 Share Posted November 25, 2006 well then you are just going to have to go through and add a \ to all of your quotes then. And next time I suggest you be more consistent with your coding, instead of randomly using single or double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129962 Share on other sites More sharing options...
heckenschutze Posted November 25, 2006 Share Posted November 25, 2006 Or break from PHP:[code]<?phpfunction foo(){ $myvar = "bar";?><b>$look im not php :), don't have to worry bout quotes or vars :D, print vars like <?php echo $myvar;?></b><?php}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129965 Share on other sites More sharing options...
alpine Posted November 25, 2006 Share Posted November 25, 2006 By using this method you can mix single and double quotes and no escaping is nessesary:[code]<?phpecho <<<_HTML<table width="100%" border="1"> <tr> <td align="left"> This is some $text and it's an easy way of echoing php </td> </tr></table>_HTML;// or defining it$var = <<<_HTML<table width="100%" border="1"> <tr> <td align="left"> This is some $text and it's an easy way of echoing php </td> </tr></table>_HTML;echo $var;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-129988 Share on other sites More sharing options...
wildteen88 Posted November 25, 2006 Share Posted November 25, 2006 Yes the HEREDOC syntax is recommended for this (see post above).Note when using HEREDOC syntax do not indent or put anything else on the same line as the closing delimiter (_HTML; in the example above). Otherwise you'll get an unexpected $end error message.Also when you use variables in HEREDOC you may need to wrap curly braces around them to help PHP out a little, especially with arrays (if yoou use them). Example:[code=php:0]$var = <<<EOFThis is some {$var}, when using an array you also need to use curly braces {$myarray['key_here']}EOF;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28401-solved-have-single-quotes-in-a-variable/#findComment-130029 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.