How to concatenate php variable to pass as parameter

1

I need to delete records 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 database is 192.

    
asked by anonymous 05.08.2015 / 19:21

2 answers

2

You can use "\" to merge the quotes, so 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>";
    
05.08.2015 / 19:33
3

In PHP there are some ways to encapsulate strings the main ones are using "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 in it, including variables. The single quotation mark interprets the string as literal and everything will come out as it was 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 by using single quotation marks the special character \n representing a line break is not interpreted either. In the double quotes, both the special and variable characters were interpreted correctly.

Note: The execution time of strings with " (double quotation mark) is slightly higher than ' (single quotation mark) PHP reads it to interpret it. So always give preference to ' (single quote).

    
05.08.2015 / 19:50