|What PHP Does|
|Written by Alex Armstrong|
|Thursday, 12 January 2017|
Page 2 of 2
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;
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.
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.
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:
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:
If you run this you should see:
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:
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.
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.
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.
What have we learned
Introduction to PHP
or email your comment to: firstname.lastname@example.org
|Last Updated ( Friday, 09 November 2018 )|