Jump to content

[SOLVED] have single quotes in a variable


jwk811

Recommended Posts

yes.

if you do $blah = 'moreblah'; // with single quotes

and 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 quotes

you can use the single quote inside there, because php will not be looking for the single quote to close out the string.
Link to comment
Share on other sites

By using this method you can mix single and double quotes and no escaping is nessesary:
[code]
<?php

echo <<<_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]
Link to comment
Share on other sites

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 = <<<EOF
This is some {$var}, when using an array you also need to use curly braces {$myarray['key_here']}
EOF;[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.