Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Any of the examples in the previous section is an excellent first step toward introducing
PHP into a Web site. Their impact in terms of server load is relatively low. I like to think
of sites using similar approaches as being PHP-enabled, as if they had a small injection of
PHP that makes them extraordinary. The step beyond this is what I think of as PHP-
powered: a site made completely of PHP. In this approach every byte of output comes
from PHP. The print (or echo or printf) function is used to send HTML tags.
Every page is a script inside a single pair of PHP tags.


You might have noticed that most of the examples in the book take this approach. I have
found that while this requires extra time up front, the code is much more maintainable.
Once information is put in the context of a PHP variable, it's easy to add something
dynamic to it later. It also has the advantage of ultimately being more readable as the
page becomes more complex. As a simple example, compare Listing 20.5 to Listing
20.6. The idea is to change the background color of the page depending on the time of
day.


My experience has been that having all the HTML inside the PHP script allows very
quick changes. I don't have to search for the opening and closing tags buried inside the
HTML as in Listing 20.5. It also allows me to break code up into separate lines in the
source code that appear as a single line in the output. An example is the header text. I can
enhance the readability but not sacrifice the presentation. This has become very handy
when dealing with tables. Leaving any whitespace between a TD tag and an image causes
an extra pixel to appear. In an HTML file, the solution is to run the whole thing together
on one line. Inside a PHP script I can have many print calls and send an endline only
in the last. The result is a single line in the output, but very readable source code.


The usefulness of these techniques, like that of many others, increases with the size of the
project. I've created 50-page Web applications using both approaches and can attest to the
value of putting everything inside the PHP code.


Listing 20.5 Mixing PHP and HTML




Listing 20.5/TITLE><br /> </HEAD><br /> <?<br /> $Hour = date("H");<br /> $Intensity = round(($Hour/24.0)*(0xFF));<br /> $PageColor = dechex($Intensity).<br /> dechex($Intensity).<br /> dechex($Intensity);<br /> ?><br /> <BODY BGCOLOR="#? print($PageColor); ?>"><br /> <H1>Listing 20.5</H1><br /> </BODY><br /> </HTML> </div> <meta itemprop='headline' content="p 621: Any of the examples in the previous section is an excellent first step toward introducing PHP into a Web site. Their impact in t ... - Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites - free download pdf - issuhub"> </div> <div role="navigation" itemscope itemtype="http://schema.org/SiteNavigationElement"> <span itemprop="url"><b><a href="/view/index?id=3221&pageIndex=620" rel="previous" itemprop="name">← Previous</a></b></span> <span itemprop="url" class="mx-3"><b><a href="/view/index?id=3221&pageIndex=622" rel="next" itemprop="name">Next →</a></b></span> </div> <div style=" text-align: center; margin: 20px auto; padding: 13px; width: 240px; font-size: 20px; "> <a class="page-link" style="background-color: #72bf86;" target="_blank" href="/view/index?id=3221&pageIndex=620#bookdownload" title="Free download pdf" >Free download pdf</a> </div> </div> <div class="footer"> <div class="container"> <div class="row"> <div class="col-lg-3 ml-lg-auto mb-5 mb-lg-0"> <div class="mb-4"> <h5 class="text-dark">Get our desktop app</h5> </div> <a class="btn btn-icon btn-indigo rounded-circle mr-2" target="_blank" href="/download/issuhub.dmg"> <i class="fa fa-apple"></i> </a> <a class="btn btn-icon btn-indigo rounded-circle" target="_blank" href="/download/issuhub.exe"> <i class="fa fa-windows"></i> </a> </div> <div class="col-6 col-md-3 col-lg mb-5 mb-lg-0"> <h5 class="text-dark">Company</h5> <!-- Nav Link --> <ul class="list-unstyled mb-0"> <li class="my-2"><a href="/about">About</a></li> <li class="my-2"><a href="/contact">Contact</a></li> <li class="my-2"><a href="/news/index">News</a></li> </ul> <!-- End Nav Link --> </div> <div class="col-6 col-md-3 col-lg mb-5 mb-lg-0"> <h5 class="text-dark">Features</h5> <!-- Nav Link --> <ul class="list-unstyled mb-0"> <li class="my-2"><a href="/quick">Quick Start</a></li> <li class="my-2"><a href="/desktop">Desktop</a></li> <li class="my-2"><a href="/editor-help">Editor</a></li> </ul> <!-- End Nav Link --> </div> <div class="col-6 col-md-3 col-lg"> <h5 class="text-dark">Documentation</h5> <!-- Nav Link --> <ul class="list-unstyled mb-0"> <li class="my-2"><a href="/support/index">Support</a></li> <li class="my-2"><a href="/site/pricing">Pricing</a></li> </ul> <!-- End Nav Link --> </div> <div class="col-6 col-md-3"> <h5 class="text-dark">Resources</h5> <!-- Nav Link --> <ul class="list-unstyled mb-0"> <li class="my-2"> <a href="/tutorial" target="_blank"> <span class="media align-items-center"> <i class="fa fa-info-circle mr-2"></i> <span class="media-body">Tutorial</span> </span> </a> </li> <li class="my-2"> <a href="/site/login"> <span class="media align-items-center"> <i class="fa fa-user-circle mr-2"></i> <span class="media-body">Your Account</span> </span> </a> </li> </ul> <!-- End Nav Link --> </div> </div> </div> </div> <div class="footer"> <div class="container"> <div class="row"> <div class="col-md-6 mb-4 mb-md-0"> <!-- Nav Link --> <ul class="nav nav-sm nav-white nav-x-sm align-items-center"> <li class="my-2"> <a href="/privacy">Privacy & Policy</a> </li> <li class=" opacity my-2 mx-3">/</li> <li class="my-2"> <a href="/terms">Terms</a> </li> </ul> <!-- End Nav Link --> </div> <div class="col-md-6 text-md-right"> <ul class="list-inline mb-0"> <!-- Social Networks --> <li class="list-inline-item"> <a class="btn btn-xs btn-icon btn-soft-light" href="https://www.facebook.com/Issuhub-Flipbook-2315543688769343/"> <i class="fa fa-facebook text-dark"></i> </a> </li> <li class="list-inline-item"> <a class="btn btn-xs btn-icon btn-soft-light" href="https://twitter.com/IssuhubBooks"> <i class="fa fa-twitter text-dark"></i> </a> </li> <!-- End Social Networks --> </ul> </div> </div> <!-- Copyright --> <div style="text-align: center;"> <div >© ISSUHUB. 2025. All rights reserved.</div> </div> <!-- End Copyright --> </div> </div> </div> </div> <script src="/assets/6df76c57/assets/js/vendors/jquery-3.2.1.min.js"></script> <script src="/assets/6df76c57/assets/js/vendors/bootstrap.bundle.min.js"></script></body> </html>