How to concatenate php variable to pass as parameter

Asked

Viewed 1,128 times

1

I need to delete records coming from my database but I am not able to pass the variable to perform the operation, I am not able to concatenate the variable in my code, I did the following:

$aretorno["tabela"] .=  '<td align="center"><button type="button" class="close" aria-hidden="true" onclick="DlgExcluirNota($IdFase)" data-toggle="tooltip" data-placement="auto" title="Excluir nota"><span class="glyphicon glyphicon-trash"></span></td> ';

The message on my console is this:

Uncaught ReferenceError: $IdFase is not defined
onclick @ DetalhesContrato.php:1

The existing Id in the bank is 192.

  • The variable $IdFase is from PHP, right?

  • Hello @Kaduamaral, that’s right, is a php variable.

2 answers

3

In PHP there are some ways to encapsulate strings the main ones are using the "string" (double quotes) and also 'string' (single quotes) and they are not the same thing.

The double quote searches the string to see if there are any expressions inside, including variables. Already the simple quote interprets the string as literal and everything will come out as written.

Examples:

$nome1 = 'Carlos';
$nome2 = 'Eduardo';
echo 'Olá $nome1!'; 
// Olá $nome1!

echo "Oi $nome2!";
// Oi Eduardo!

echo 'Meu nome é $nome1.\nE o seu?';
// Meu nome é $nome1.\nE o seu?

echo "O meu?\nÉ $nome2.";
// O meu?
// É Eduardo.

Note that using simple quotes the special character \n which represents a line break, is also not interpreted. In double quotation marks, both special characters and variables were correctly interpreted.

Obs.: The running time of strings with " (double quote) is slightly larger than the ' (quote), because PHP reads it to interpret it. So whenever possible give preference to ' (quotation mark).

  • Thanks for the excellent explanation and code, thanks to @Kaduamaral.

2


You can use the " " to interlink the quotes, so the concatenation is done:

$aretorno["tabela"] .=  "<td align=\"center\"><button type=\"button\" class=\"close\" aria-hidden=\"true\" onclick=\"DlgExcluirNota($IdFase)\" data-toggle=\"tooltip\" data-placement=\"auto\" title=\"Excluir nota\"><span class=\"glyphicon glyphicon-trash\"></span></td>";
  • 1

    Baill, boy, I’m sorry, after all this time I’ve come to thank you for your great help,.

Browser other questions tagged

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