2013-03-09

What EOT is.~

If you want to know what EOT is, you need to know first, what 'here-doc' is. Using the simplest words, 'here-doc' (also called 'here document') is the way to delimit strings. It preserves the line breaks as well as other whitespace in the text. Jeffrey Way shows me some nice examples of presenting strings in PHP:


                               ?php>
                               $info = array(
                               'name' => 'Caroline';
                               'surname' => 'Smith';
                               'gender' =>  'Woman';
                               );
                               ...
// If we have an array with some strings inside, which we would like to present, we can use this form:

                              ...

                              $result = "<h2>{$info['gender']}</h2><p>name is:</p><h3>{$info['name']}{$info['surname']}</h3>";
                             echo $result;
                             ...

// Or this form:

                             ...

                             $result = "<h2>".$info['gender']."</h2>";
                             $result .= "<p>name is:</p><h3>".$info['name'];
                             $result .= $info['surname']."</h3>";
                             echo $result;
                             ...

// Or we can use 'here-docs', which will delimit my strings, this way:

                             ...

                             extract($info);
                             $result = <<< EOT
                             <h2>$gender</h2>
                             <p>name is:</p>
                             <h3> $name $surname</h3>
                             EOT;
                             ?> 

The results will be, in every way, the same: Woman name is: Caroline Smith  

However, we need to remember, that if we want to EOT work in this case, we need to remember about two things. To put at the beginning of the code, 'extract' function, which will import variables from an array into the current symbol table (so that, we don't need to write in our code, $variable['array_element'] but only concrete variable, that we need, so the code looks cleaner), and at the end of the code, on a new line, we need to put EOT, in the same way, as I wrote above.
 

Brak komentarzy:

Prześlij komentarz

Dziękuję Ci za poświęcony czas i pozostawienie komentarza :)