How to use double and single quotes?

Asked

Viewed 3,580 times

10

Let’s imagine the following example:

$color = "red";
echo ("<button onclick='change_background("/red"/);'>$color</button>");

What should I use when I have 3 levels using the example in change_background("/red"/);?

I wanted to know how it works when they’re inside each other like "primeiro nivel 'segundo nivel \'terceiro nivel\' '"

  • echo is not a function, so parenthesis is optional.

3 answers

8


You need to read these two questions (and answers):

So it’s good to know that you can, in most situations, set a standard to avoid confusion between PHP and JS code. Thus avoiding the character of escape (to \).

This character is only needed when they get confused. For example. You are using double quotes in PHP text. And inside there’s HTML code with double quotes as well. These quotes there in HTML will close the PHP text and it’s not what you want. So use \" or \', so the PHP interpreter will know that those quotes are part of the text and it is not the indicator that it is being terminated.

It’s boring to do this, so it’s best to choose one type for the code of one language and the other type in the other code.

I usually use single quotes in PHP and double quotes in HTML, except in the rare specific cases this can be worse.

When there is JS code inside the HTML, there is not much way, you have to go back to using the same kind of quotes that HTML already used or that PHP already used and have to use the character of escape.

This is why it is better to leave JS code out of HTML as much as possible.

Note that in your example code you are not using the backslash which is the escape. In the description of the levels problem, you used correctly.

Escape characters from PHP.

Escape characters from JS.

  • but I wanted to know how it works when they’re inside each other like "first level 'second level' third level' '"

  • I was finishing the answer, see if the conclusion helps. I had even given the introduction to this. There’s something else I want to know?

  • so my third level is correct?

  • Yes, it is, I prefer to step in, to be more visible. I prefer to avoid having the third level.

  • Perfect thank you :)

6

The problem is in your bars, which are reversed.

Test like this:

$color = "red"; 

echo ("<button onclick='change_background(\"red\");'>$color</button>");

Change the bars from "/" to "\".

4

To solve this type of problem I usually use the function sprintf, present in the PHP. 'Cause then I can write the block of text normally.

PHP

$button = '<button onclick="change_background(%s);">%s</button>';
$color = 'red';
echo sprintf($button, "'{$color}'", $color);

JS

if (!String.prototype.sprintf) {
  String.prototype.sprintf = function() {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
      ;
    });
  };
}

var str = '<button onclick="change_background({0});">{1}</button>';
var color = 'red';
echo str.sprintf('"'+color+'"', color);

source

  • It’s easier for you to change the sprintf() that prints nothing it is suitable to make assignments on variables, by printf().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.