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

PHP is a language that creates web pages, but exactly how does it do this? If you know some HTML, you might be wondering what PHP have to do with it and how it integrates into a web page. In this article we look at the idea behind PHP. 

Banner

 

It is assumed that you already know how to enter and run a PHP application. If not then consult Getting Started With NetBeans PHP or Beginning PHP with Eclipse which explain how to download, install and use everything you need all the way to the first Hello World program.

What PHP does

You can think of PHP as a general purpose computing language if you want to, but it was designed with one task in mind and it is almost exclusively used for that task - generating web pages.

So while it might be more flattering to PHP to introduce it in the widest possible context this would be misleading and it would make the job of learning how to use it harder than it needs to be.

So let's say the obvious to make it 100% clear.

  • PHP is a language that creates web pages.

What this means in practice is that a PHP program's objective in life is to generate HTML or JavaScript or anything else that you might find in a web page. In most cases and certainly when you are first learning PHP the web technology that is used is HTML.

Again to state the obvious:

  • The output of a typical PHP program is HTML

This means that to make any sense of PHP you also have to know about the web technology that the program is generating, and in particular HTML.

In practice this shouldn't be a huge problem because HTML isn't difficult and mostly the way that PHP makes use of it is fairly simple. However, it is important to know that it is possible that you could have a problem with understanding a PHP program simply because you can't understand the HTML it is generating.

PHP as web server - CGI

The first thing to get straight is how a PHP program gets the HTML it generates to the client web browser that requested it.

This is surprisingly simple.

In the usual course of things a user asks for a web page, MyPage.html say, and this request is sent to the web server which finds the file MyPage.html stored locally on disk and sends it to the browser. So all a web server is doing is finding and sending pages stored on disk to the client web browser - this isn't a very sophisticated or intelligent piece of behaviour.

 

browser1

 

However, this isn't the only way that a web page can be served to a client. If the web server is asked for a web page that ends in something that it recognises as an executable program, MyPage.exe for example, then instead of sending he file MyPage.exe to the client in the hope that it might contain HTML after all the web server loads and runs the program on the server and collects whatever its output might be and sends that to the web browser.

 

browser2

 

Of course this allows the web programmer to create a program that generates HTML and have this executed and its output sent to the browser without the user really being aware of any difference - except perhaps the use of a "funny" file name.

This is the CGI or Common Gateway Interface and it has been in use for a long time as the basic way of generating web pages. Today there are some improved versions of the CGI designed to make the process more efficient, more powerful or, in most cases, just to hide the fact that this is what is going on from the programmer.

Web development systems such as .ASP, PHP and so on essentially are still using the CGI idea even if the mechanism has been tinkered with.

To return to PHP this is exactly how it all works.

You write a PHP program, MyProg.php, and when the client asks for this as if it was a web page then the server loads and runs the program MyProg.php and sends any output it creates as if it was HTML to the web browser client.

Notice that the server doesn't check the vailidity of the program's output in anyway. The program can produce complete rubbish and the server will still pass on its output.

Also notice that the PHP program isn't running on the client's machine but on the server and all it can do is send the client some data that will be rendered as HTML. That is, PHP is a server side language - it runs on the server and its interaction with the client is limited to sending it a web page.

You might think that server side languages are limited, and in a sense they are when it comes to user interaction, but there are ways around this problem.

First we have to master the basics.

Generating HTML

There are two distinct ways that PHP allows you to generate output to be sent to the client and these two are confusing and cause a lot of problems for programmers.

A program consists of a list of instructions that are obeyed one after another. You tell the machine to do something, it does it and then you tell it to do something else.

So for example, a very simple PHP program, which is a slight modification of the "Hello World" program inGetting Started With NetBeans PHP - Local Projects or  Beginning PHP with Eclipse, is:

<?php
 echo 'Hello';
 echo 'PHP' ;
 echo 'World';
?>

The instructions that make up the program are three "echo" commands which simply take whatever you have entered between quotes and send it as output to the browser.

Notice that a PHP program always starts with <?php and ends with ?> and that these two tags bracket the list of instructions. Also notice that every PHP instruction has to end with a semicolon - if you forget the semicolon you will see an error message.

If you run this program, remembering to start the Apache web server if necessary, then the web browser will show:

HelloPHPWorld

Notice that running the program is just a matter of providing the appropriate URL to a browser -

http://localhost/Learn%20PHP/Hello.php,

for example. The simple act of the browser asking the sever to load the web page called Hello.php invokes the CGI mechanism and runs the program returning to the browser any output it generates as the web page.

At this point you might question the idea that the PHP program has output HTML - but plain text without any markup tags is valid HTML and any browser will show the text unformatted using a default font etc.

You might also notice that there are no spaces between the words. This is just a result of the fact that what you send to the client browser is exactly what you typed - where are the spaces between the words?

To make it look better change the program to have a space at the end of each word.

Direct text

The echo command is one of a number that send output to the browser and they all have their uses but you can send output to the browser without any commands at all.

If you simply include some text in the PHP program that is outside of the <?php and ?> tags then it is simply sent to the browser unmodified. So you could have written the previous program as:

Hello PHP World

with no sign of a PHP command anywhere and no <?php ?> tags - you don't even need quotes around the text.

The rule is:

  • Any text not enclosed in <?php ?> is simply passed to the output as if it was within an echo instruction.

This makes it possible to include blocks of HTML, or text that is part of the web page, that never change and isn't processed by PHP commands.

You can even interweave text and PHP as and when it is needed. So you can have some text, a block of PHP, some more text and then some more PHP and the result is sent to the output in the same order. For example:

Greetings
<?php echo 'Hello';?>
-------------
<?php echo 'PHP' ;
  echo 'World';?>

This produces:

 

fig1

 

Notice that all of the text is on a single line even though it looks as if it should be on separate lines. The reason is that a web browser ignores any line breaks in the text that is sent to it. If you want to format text in a web page then you have to use HTML tags to do the job. In particular if you want a line break you need to use the tag <br/> . For example:

Greetings <br/>
<?php
echo 'Hello <br/>';
?>
-------------<br/>
<?php
echo 'PHP <br/>' ;
echo 'World <br/>';
?>

which has line breaks after each word.

Notice the two different ways that the <br/> tag has been inserted - just as part of the text of the page and within an echo instruction. The effect is the same however and the <br/> tag is sent to the output and hence to the client browser with the result:

 

fig2

Banner

<ASIN:1491918667>

<ASIN:0134291255>

<ASIN:1491905018>

<ASIN:0321784073>

<ASIN:0321833899>

<ASIN:144936375X>



Last Updated ( Sunday, 15 January 2017 )
 
 

   
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.