I have a problem with quotes in my page, I have a variable like this <?php $datafile = " <td><font size=-1> <p><i>This is some aritcle in a web page</i><br><br><p>Los Angeles, CA July 17, 2004 -- this is the rest of "article" and to see the rest </p><href="http://www.mysite.com target= "_blank">click here </a> this is the rest of the text.</p></td> "; <? PHP: as you see my vaiable starts with " and then some text, a link and ends with "; the problem is my text has some quotes in it also, a word in between quotes and link, this makes problem when page loads, it gives error when I remove the quotes from the text and link then it works, how can pass this problem? thanks.
A couple of ways: 1) make your variable value delimiters single quotes instead of doubles. This will work until you embed other PHP variables in that value and expect it to parse. 2) escape the double quotes inside the string with a backslash (that is, put a single backslash before each double quote inside the value).
You can try this: <?php $datafile =<<<HERE <td><font size=-1> <p><i>This is some aritcle in a web page</i><br><br><p>Los Angeles, CA July 17, 2004 -- this is the rest of "article" and to see the rest </p><href="http://www.mysite.com target= "_blank">click here </a> this is the rest of the text.</p></td> HERE; ?> PHP: You just have to make sure there is no space before the ending HERE; tag. You can use any word in place of 'HERE'. You just have to make sure that the word doesn't appear in your text,followed by a semicolon. So if you use HERE, the phrase 'HERE;' (without quotes) should not appear within your text. You can even use : $foo = <<<BLAHBLAH yadda yadda yadda BLAHBLAH; PHP: Thomas
thanks folks, great, I like the last option man, but little confiused over if any of the signs should not appear or all together should not appear within text, if I use ### so if one # appears could cause problem? or if I use ###blah could also blah or just a # make problem? how about the <<< sigins? thos are optional as well? could I make <?php $datafile = ### <td><font size=-1> <p><i>This is some aritcle in a webpage</i><br><br><p>Los Angeles, CA July 17, 2004 -- this is the rest of "article" and to see the rest </p><href="http://www.mysite.com target= "_blank">click here </a> this is the rest of the text.</p></td>###;<? PHP: thanks
does the backslash works with value of variable as well? I could not make it work, I use: $datafile=\'this is my text\'; and also $datafile=\"this is my text\"; dose not work, I tried with other option as well, and won't work, $datafile = <<<BLAHBLAH yadda yadda yadda BLAHBLAH; PHP: thanks for any help
The backslashes are ONLY for the non-delimiting quotes: that is, all but the start and end ones. If you're going to use heredocs (the <<<BLAHBLAH way) whitespace is important. There's no whitespace between the <<< and the BLAHBLAH, so your final BLAHBLAH has to be on a new line, without space before the BLAHBLAH.
I think its easier just to follow the 2nd post and use single quotes. $myvar= '"This is easier for me and im new to php."' PHP: Also if you need to use another variable inside the text, you end the string and combine the variable and string with a dot. For example: $text= 'A quote by someone: "'.$somequote.'" -'.$quoteauthor; PHP:
thanks for reply; I have already tried with a single quote, and did like you did start with a single quoet and then double quotes, still does not make any changes, here is how i did; <?php $datafile = '"this is the main article that is being tested,this is the david's article that is being benefits from it. Good Luck."'; PHP: in the text I have david's article when I run it gives me error: Parse error: parse error, unexpected T_STRING in c:\Inetpub\wwwroot\555.php on line 2 as you can see in the code area the text after single quote is not red anymore, that is how it appears in my php editor( Edit plus) as well.
I guess the best solution is coderlinks's solution... it so happened only that you type it incorrectly that is the reason why it does not works.
OK, it is working now; not only it has to be no space after the HERE in the end, it has to have a line feed after =<<<HERE also, no space either, right after =<<<HERE hit enter. thanks
No offence greenworld, but seriously... I told you at the start that you needed to escape any quotes. Did you not think that the same rule applied if you used single quotes? Extend what I told you and you would do... $datafile = '"this is the main article that is being tested,this is the david\'s article that is being benefits from it. Good Luck."'; Code (markup): Escape your single quote and your fine. I thought I would have made sense, but apparently I don't. Anyway, glad you got your heredoc working but as you've found, they're harding than learning to escape a quote, ESPECIALLY when PHP tells you what the problem is with the missing backslash... not so with the heredoc. The parse error told you exactly what the problem was and where it was. It's your code and your call, but to me, heredocs are an ugly solution that have more 'cons' than 'pros'.