1

Let's say this is my script down here, as you can see I've used multiple " and '. These quotations conflict in ending the current php variable, so it basically sees this:

$message = "<?php echo '<div class="

As a string, whilst the quotation is only to define the class, not to end the variable. I've tried using ' but then it conflicts with the echo, so I'm kinda stuck at the moment.

    <?php

    $message = "
    <?php
    echo '<div class="gebruiker">';
    $fh = fopen('_gebruiker.txt','r');
            while ($line = fgets($fh)) {
              echo($line);
            }
            fclose($fh);
    echo '</div>';
    ?>
    ";

**MORE PHP CODE HERE**

?>

How can I use multiple quotations in one PHP script without them having conflicts.

branco holtslag
  • 257
  • 2
  • 15

5 Answers5

2

Adding a backslash so PHP can recognize just only double quotes or quotes when escaped.

Example:

echo "<div class=\"gebruiker\">";
bcesars
  • 1,016
  • 1
  • 17
  • 36
  • Escape orgies result in rather unreadable code. Also, he needs to escape variables, too, if he's using double quotes. – ThiefMaster Jan 15 '15 at 13:24
  • This is the easiest way he can use it. Also, there other ways we can treat it. But for a specific simple line, escaping with backslash suits better. You just cannot disregard my answer and this option. – bcesars Jan 15 '15 at 13:45
  • I agree with @ThiefMaster, still this is an easy solution, for an asked question that has some flaws in its approach (IMHO). – Masiorama Jan 15 '15 at 13:45
  • But he cannot use that solution! His example string contains single quotes and dollars, so he at least needs to escape the dollars, too... – ThiefMaster Jan 15 '15 at 13:47
  • @ThiefMaster Yes that is true, the variable $fh was not taken in the code, it simply removes it. Still looking for a solution for that, but atleast I got one of the problems solved now – branco holtslag Jan 15 '15 at 14:15
2
  • If you use single quotes outside, you need to escape all single quotes inside, but can use double quotes and the dollar char without escaping.
  • If you use double quotes outside, you need to escape all double quotes and dollar chars inside, but can use single quotes without escaping.
  • If you use a heredoc string, you need to escape dollar chars but can use both single and double quotes without escaping.
  • If you use a nowdoc string, you do not need to escape anything unless you have FOO; in the string at the beginning of a new line.

So the solution is to use a nowdoc string:

$message = <<<'EOF'
your stuff with " or ' or $ here!
EOF;
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

I suggest using either Heredoc or Nowdoc

Example

$foo = 'test';

// Heredoc
$here = <<<HERE
I'm here, $foo!
HERE;

// Nowdoc
$now = <<<'NOW'
I'm now, $foo!
NOW;

Heredoc will print the contents of $foo when echoed while Nowdoc will simply echo $foo.

In the references I added below you can do more reading up on this subject.

References:

Community
  • 1
  • 1
Peter
  • 8,776
  • 6
  • 62
  • 95
0

You can escape the quotes with a slash character like so: \"

Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
0

This should work, you can't open Php tag without closing before.

<?php

$message = '<div class="gebruiker">';
$fh = fopen('_gebruiker.txt','r');
        while ($line = fgets($fh)) {
          $message .=$line.'<br/>';
        }
        fclose($fh);
$message.='</div>';
echo $message;
**MORE PHP CODE HERE**
?>
mgamon
  • 308
  • 7
  • 16