David Beroff (d4b) wrote,
David Beroff
d4b

  • Mood:

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
   }
?>
Tags: mtat, sak, webdev
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 2 comments