How can they be done templates
in a string with PHP how they are done in Javascript? I mean the following
var pez = {
color: "azul",
tamaño: {
largo: "20cm",
ancho: "3cm"
},
origen: "México"
}
document.getElementById("pez").innerHTML = `
El pez ${pez.color} mide ${pez.tamaño.largo} de largo y ${pez.tamaño.ancho} de ancho <br>
Vive en ${pez.origen}
`;
<div id="pez"></div>
What I currently do to do something similar is like this
$dia = "10";
$mes = "Septiembre";
$año = "1999";
echo "Hoy es ".$dia." del mes ".$mes." del año ".$año;
I don't think it's the best way to do it and it also becomes more involved in sql queries. How can I optimize this kind of tasks?
In PHP there are four ways to wrap a string with
'Comillas simples'
,"Comillas dobles"
,heredoc
andnowdoc
:Single quotes: Shows the string as it is written:
Double quotes: Basically it allows to interpret
secuencia de escape
and variables:You can achieve this by printing the variables inside the text string thanks to the syntax of:
{$variable}
I leave you an example very similar to what you do in JavaScript
I think you mean this: