What PHP Does
What PHP Does
Written by Alex Armstrong   
Thursday, 12 January 2017
Article Index
What PHP Does
Dynamic Web Pages

Mixed HTML And PHP

You can see that the problem with this approach to building a PHP program is that it is very messy. You have PHP, HTML and plain text intermingled and this makes it very difficult to read. When if comes to programming the simpler things are, the better because there is less likelihood that a bug will use the mess to hide.

For example, in the real world most of the "boilerplate" HTML, i.e. the standard stuff, tends to be put into the PHP page directly and then PHP code is inserted into the page where HTML needs to be generated.

For example, the Hello World program would probably look more like;

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>My Demo</title>
 </head>
 <body>
  Greetings <br/>
  <?php
  echo 'Hello <br/>';
  ?>
  -------------<br/>
  <?php
  echo 'PHP <br/>' ;
  echo 'World <br/>';
  ?>
 </body>
</html>

You can see that this looks messy and it is very easy for even a simple PHP page to become very complex and difficult to maintain. Even so this mixed HTML with PHP scattered though is almost the standard way to use PHP and it certainly was the way that early PHP language was intended to be used. You can think of it as writing an HTML page which provides the static content and then adding to it small chunks of PHP which generate the parts of the content that should change for each user or client request.

There are ways to make the generation of a web page appear much simpler and more straightforward to follow but it involves some more advanced ideas. For the moment you need to keep in mind that the examples that follow might not be written in the best possible way for real use but in the best possible way to demonstrate some point or idea.

The key point to keep in mind is that the purpose of a PHP program is to generate a web page and the HTML, text or whatever can be embedded into the program or generated by echo or similar instructions.

Some programmers like to think of a PHP program as a web page that happens to have some PHP instructions embedded in it at various places. You might find this helpful but thinking about a PHP program as something that generates text and HTML to be sent to the browser is closer to reality.

Today's web page

At this stage of describing PHP it is always difficult to find a convincing example that doesn't use facilities that are much more advanced.

Banner

The whole point of PHP is to generate dynamic web pages and in most cases this involves the use of a database or something to retrieve data that can then be rendered as a suitable web page.

For an example of something that you can do using just HTML, consider creating a page that shows the user the current date and time. This is something that cannot be done with pure HTML on its own - you need either a client-side or a server-side program to generate the text representation of the current date and time.

A client side program requires that the browser supports whatever technology you have chosen, JavaScript say,  but if you use a server-side language like PHP then the browser has to handle nothing but pure HTML.

The key to creating a web page that displays the current date is to use the PHP command date. This is, strictly speaking, known as a function and later you will discover how to extend PHP by adding your own custom commands or functions. For now all you need to know is that the date function returns the current date formatted as you specify.

For example, date('y-m-d') gives the data formatted with a two-digit year, month and day value separated by dashes e.g. 17-01-09 which is the January 9th, 2017.

The simplest program that will display today date is:

<?php
echo date('y-m-d');
?>

but this doesn't look very impressive. If you run it you should see the date in a default font. A slightly more complicated program might include some text to announce the date and could enclose everything in <h1> tags to make it use a headline style:

<?php
echo '<h1> Todays date is: ';
echo date('y-m-d');
echo '</h1>';
?>

If you run this you should see:

 

date

 

What is important about this example is that you can see that the program generates the following HTML:

<h1> Todays date is: 17-01-09</h1>

and you should be able to see where each element of the web page comes from.

Also notice that once generated the HTML doesn't change - this is not a "live" display of the date in the browser. You can see this more clearly is you also include a specification to display the time:

<?php
echo '<h1> Todays date is: ';
echo date('y-m-d:h:m:s');
echo '</h1>';
?>

This now displays the date with the hours, minutes and seconds. Notice that the seconds do not update as you look at the web page only when then web page is refreshed and the PHP program is run again is the date and time up-dated.

datetime

In a real example we would need a lot more PHP to generate a lot more HTML - the HTML in this example isn't finished as it doesn't have a header or a body or quite a few other things that every well-formed web page should have. The point is that it is a simple example stripped down to just what is necessary.

Where Next?

Now you have the basic idea of what PHP is for and how it generates HTML you are a long way towards learning and using PHP. Next you have to find out about the basics of the language and some of its features and facilities. The difficulty is that modern PHP really is a fully fledged object oriented language and there is a lot to learn if you hope to master its use. Then there is the all important interaction with a database. Nearly all but the very simplest web sites makes use of a database to store their content and PHP is used to find out what the user wants and retrieve it from the database. The PHP then formats the raw content into something pleasing and shows it to the user.

This is such a common approach that it makes good sense to use off the shelf implementations of this idea. Such a system is generally called a Content Management System or CMS and many well known CMS such WordPress and Joomla make use of PHP and it might well be a good investment to learn enough PHP to make sense of how these programs work. Instead of using PHP to build a web site from scratch it is usually more efficient to use a CMS and then use your knowledge of PHP to customize the result. You still need to know PHP but perhaps not as extensively as you might otherwise for a project as ambitious as building a web site from scratch. 

 

phplogo

What have we learned

  • A PHP program is run when a client asks for it to be displayed as if it was a web page.

  • Any output that the PHP program generates is sent to the web browser and hence treated as a web page.

  • Anything between </php ?> is treated as a list of PHP instructions which are obeyed one after another.

  • Each PHP instruction ends with a semicolon

  • Anything not between </php ?> is simply sent to the client browser as output of the program.

  • The echo instruction sends whatever follows it (in quotes) to the output.

  • The date function can be used to generate the date and time when the PHP program is running.

 

Related Articles

Getting Started With NetBeans PHP - Local Projects 

PHP Inner Functions And Closure

Ten minutes to PHP objects

Advanced loops

Object-oriented HTML Generation In PHP

Ten Minutes to PHP

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.

 

Banner


Peachpie Open Source PHP to .NET Compiler

Peachpie is a new open source PHP language to .NET compiler, which aims at full PHP 7 compatibility. Looking at it gave us the opportunity  to revisit the state of dynamic language interoperabili [ ... ]



PHP Inner Functions And Closure

PHP inner functions and anonymous functions are a little strange to say the least. However, just because something is strange doesn't mean that it isn't useful. We take a close look at the way PHP fun [ ... ]


Other Articles

 
 

 

blog comments powered by Disqus

<ASIN:1449392776>

<ASIN:0994346980>

<ASIN:0596006306>

<ASIN:1785883445>

<ASIN:1890774790>

<ASIN:1484219953>



Last Updated ( Sunday, 15 January 2017 )
 
 
Banner

   
RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.