How to display echo quotes in PHP?

Asked

Viewed 14,096 times

6

I am developing an application that uses PHP + AJAX and I came across an annoying problem. AJAX only recognizes the function if it is like this:

onclick="remover_musica_ftp('0','Deposito de bebida'
);"

I’m using PHP to "print" these values in the browser like this:

echo "<a href='#excluir' class='button icon-trash' onclick=remover_musica_ftp('0','".$nomes."');  title='Excluir'></a>";

I have to use php because I’m making one foreach. So far so good, the problem is that when the word contains spaces, ajax simply does not respond, because the code would need to be like in the first example, only when I add the quote " automatically PHP becomes invalid, as the same echo started with an quotation mark. Does anyone know what I should do in this case? If I open the echo with a ' error the same way, because it would need to invert the ajax onclick that would not allow the 'in office. Give me a suggestion!

  • 1

    You need to escape the quotation marks so that PHP realizes that this "quotation mark" is not for PHP. Test like this: echo "<a href='#excluir' class='button icon-trash' onclick='remover_musica_ftp(\"0\",\"".$nomes."\");' title='Excluir'></a>";

  • 1

    I don’t know if it will, but this part, ('0','".$nomes."'), can be written as ('0','$nomes'). PHP parses the variable when it is inside double quotes.

  • But no need to double quote string?

2 answers

6

When you want to show the same type of quotes you are using to enclose the string, you need to escape it using a slider \

$string = 'aspas simples: \' e aspas dupla: " ';
$string = "aspas simples: ' e aspas dupla: \" ";

A situation that can occur is that you have to escape a quotation mark inside an already escaped string, so you will need to put an escaped bar, as in the ex. down below:

echo '<a onclick="remover_musica_ftp(0,\'It\\\'s Name Is\');"></a>';

// retorno:
<a onclick="remover_musica_ftp(0,'It\'s Name Is');"></a>

5


You need to escape ".

$nomes = 'nomes...';
echo "<a href=\"#excluir\" class=\"button icon-trash\" onclick=remover_musica_ftp( 0 , \"$nomes\" ); title=\"Excluir\">link</a>";

Output

<a href="#excluir" class="button icon-trash" onclick=remover_musica_ftp( 0 , "nomes..." ); title="Excluir">link</a>


If the argument before the name is numerical, you need to use '

remover_musica_ftp( integer , "string" )
  • I had forgotten to escape, really thank you!

  • 1

    I’ve also forgotten a lot and it sucks not being able to plique (')

Browser other questions tagged

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