July 26th, 2013

confident

Turning PHP on and off in unexpected places

I was back poking around boutell's Accountify, when I learned something new:
PHP allows us to shift in and out of "PHP gear" any time we want... even right in the middle of an if statement.
(Source)

So I decided to try this... and it works! :-) I took the code that "activates" the opportunity map on pages of SafeAmericanKids.com that need one, and replaced the mildly-messy code (including echoes, escaped quotes and newlines)...
<?php
if ($map)
   {
   echo "<script src=\"http://maps.google.com/maps/api/js?sensor=false\"\n";
   echo "type=\"text/javascript\"></script>\n";
   echo "<script src=\"/includes/StyledMarker.js\" type=\"text/javascript\"></script>\n";
   }
?>

... with a slightly-less-messy version that seamlessly flows from PHP to HTML and then back to PHP:
<?php
if ($map)
   {
?>
   <script src="http://maps.google.com/maps/api/js?sensor=false"
   type="text/javascript"></script>
   <script src="/includes/StyledMarker.js" type="text/javascript"></script>
<?php
   }
?>