Strange expressions in PHP

Asked

Viewed 47 times

3

Someone explains why this:

<?php

   $world = _("World"); 
   $str = <<<EOF
   <p>Hello</p>
   <p>$world</p>
EOF;
    echo $str;

?>

generates this:

Hello

World

error-free?

what would that be <<<, EOF, <p>$world</p>, _("World");. Finally I wanted to understand what these things are that I’ve never seen developing in everyday life PHP

We can define anything after <<< EOF?

It would be right to develop PHP with html using this? if yes why? if not why ?

Example, instead:

echo "<table><tr><td>".$titulo."</td></tr></table>";

or so:

<table><tr><td><?php echo $titulo ?></td></tr></table>

Write that:

//tabela
$minha_tabela = <<<EOF
<table>
 <tr>
  <td>titulo e $titulo</td>
 <tr>
<table>    

EOF;
//apenas dando esse echo mostro minha tabela aonde eu quero
echo $minha_tabela;
  • As to the _('world'); or it may be a user-defined function with that name or it is the extension to add translation to the site called gettext

  • still has <p>$word</p> without echo

  • the <<<QUALQUERCOISA $variable QUALQUERCOISA; will concatenate the variables you pass when you use the echo. Is the same as sprintf("Hello %s", $world) or echo "Hello $world";

  • The <p>$word</p> this inside the EOF, so it is not PHP expression, but HTML saved inside the value of the variable

  • 2

    Being correct or not is very relative. Actually these <<<QUALQUER_COISA is just one more way to print multiple lines with an interpolation/concatenation of variables. https://pastebin.com/xHn9bm2q . I use it this way when I do something in cli.

No answers

Browser other questions tagged

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