RU beehive logo ITEC dept promo banner
ITEC 325
2019fall
ibarland

php strings

video: php-strings (18m34s)

PHP’s implicit-echo-mode vs. PHP-mode

In PHP, every line not inside “<?php ?>” is actually an implicit echo statement! For example, the following two programs produce exactly the same output.

<?php
echo "hello world\n";
echo "bye now.\n";
?>
                   
hello world
bye now.
That’s right, the text on the right is a complete php program, and when you use php to run the program, it is using this new rule that everything is an implicit-print-statement, if it’s not inside “<php” and “?>”. A better example, where most of the program is implicit-prints, but a few places do some computation, would be:
<?php
$title = "quick example";
echo "<html>\n";
echo "  <head>\n";
echo "    <title>", $title, "</title>\n";
echo "  </head>\n";
echo "  <body>\n";
echo "    <h3>$title</h3>\n";
echo "    <p>How are you?</p>\n";
echo "  </body>\n;
echo "</html>\n";
?>
<?php $title = "quick example"; ?>
<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body>
    <h3><?php echo $title; ?></h3>
    <p>How are you?</p>
  </body>
</html>
Both of these two programs, when run as php programs, will print out the same characters. (The fact that what it prints happens to be html is irrelevent, from php’s perspective.)

If you happen to want to write many programs which print, and large swaths of what you want to print are constant but there are occasional places where you want to tailor the output, then yes, it is extremely convenient to have a language where most everything is implicit-printing, and a short prefix brings you to a "computing" mode. Printing web-pages is exactly such a use-case. I’ll posit: the reason PHP is so popular, despite so many egregious design flaws, is (a) implicit print-statments, and (b) the ease of embedding variables inside strings with $.

even better: Unsurprisingly, PHP also gets details wrong with its two hit features2. Others have taken these ideas and implemented them properly: pollen is an authoring system (slightly web biased) based on top of racket’s “scribble” syntax (a fully general-purpose setting for programs whose body is mostly fixed text/strings).

Many people are under the mistaken impression that “php programs are embedded inside html”. That is misguided: there is nothing special about html, and technically the whole thing is a program where most lines are implicit-prints. There is no "embedding", even if it looks like it.


1 A similar term is “transclusion”, including one document inside another, like Wikipedia-markup calls it.      
2 In PHP, cannot nest implicit-print mode; it prints and doesn’t return strings; variable-inclusion takes more work for array-lookup and general expressions.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.