Programming in PHP

4.28 (118)

Introduction

Over the years, PHP has become a very popular 'programming language' due to the astonishing ease of use of PHP and the power it provides. Currently there probably exists hundreds of articles on how to write PHP code and maybe another hundred or so on general programming constructs. This article aims to provide both, hence how to program with a strong focus on programming in PHP. It will provide a basis for getting started with PHP as well as understanding what is going on, so that you can apply this to other programming languages.

It is important that you know exactly what PHP is. PHP (PHP: Hypertext Preprocessor – a recursive acronym) is a HTML embedded scripting language. This means that the statements you write in PHP are contained alongside with HTML code, and thus are compiled every time a webpage is requested. PHP's syntax is very much like the syntax of C or C++ but has its own style.

The syntax of a language is the way a language is formatted. You could think English is to grammar as PHP is to syntax. This defines how the PHP is written and if it is correct or not. Upon running the script, it must be compiled, which is converting the code you have written into computer understandable code. During this stage, the syntax of you code will be checked so that the compiling can continue.

Previously, I mentioned that PHP was a HTML embedded scripting language. The term HTML embedded means that we can write PHP code alongside with HTML code, and store them both in the same file. Therefore, when the server processes the PHP script, only the PHP code will be run and the HTML code will be left alone. Here is a simple example of HTML embedded code:

HTML Embedded code
<html>
<body>
<?php
echo 'My name is Bob';
?>
</body>
</html>


Notice here that PHP code is contained within <?php and ?> tags. These are compulsory and without them, the PHP code will be parsed as plain old HTML code. This above script is pretty useless. It's a static script, and hence would not actually need PHP to work, as you could replace the PHP with HTML. PHP is used to create dynamic web pages. This is a web page whose output depends on the input to the page.




The above image illustrates the difference between the process of static (HTML) and dynamic (PHP) pages. It can be seen that a PHP page must first be parsed for PHP before being outputted like a normal static page. This is done by the web server and the PHP module, see www.php.net for more information. It is not necessary to understand all this information, however, may provide useful in the future. As you can see, PHP pages have an extension of .php instead of .html. If you want you server to parse a webpage as PHP code, then you must have the .php file ending.

In the following pages, the syntax of PHP will be explained as well as the underlying concepts in programming. This includes program structure, program constructs and data representation. It is assumed in this article that you have access to a server with PHP installed or have you own development environment. By the end of this article, you will be able to produce PHP pages which can take user input, process it and produce outputs based on the input received.
Rate this article: BAD 1 2 3 4 5   GOOD
Page 1 of 10    >>

Build Your Own Database Driven Website Using PHP & MySQL

  • Installation instructions for Windows, Linux and Mac OS X
  • Instantly apply working code examples from the book to your Website
  • Build a working Content Management System from scratch
  • Master MySQL database administration
  • Fully updated for PHP 5

       Download FREESample Chapters Now!

Ads

PHPNerds Newsletter