Why is PHP so important?
PHP is a server-based programming language used to turn static content webpages into dynamic content webpages. PHP has been so successful that it has become one of the most widespread programming languages used in the world, despite it is not the only language that has been developed for achieving its purpose. Because it is a free, open source and multi-platform software, it has become popular these years and most job offers for a web developer require the knowledge of this language, while other languages such as Perl or ASP have less demand. Because of its popularity and long career (since 1995), PHP has today a wide documentation available.
What should you know before attempting to learn and test PHP programming? Well, first of all, this is a server-side interpreted language. It is interpreted in servers, and not in client computers, so to test the scripts that you write in PHP, you must either have server software installed in your computer or upload your pages to any server that you own that supports PHP scripts. Today, even the most part of free hostings support PHP, so as far as you have access to a fast way to upload and replace files in the server (FTP software or a powerful web-based file manager) it should be comfortable to test your PHP scripts if you don't want to install server software in your personal computer.
Now some facts about PHP... I can start by saying that PHP syntax is notorius unpleasant. It is filled with every kind of weird character imaginable, being necessary patience and attention to detail when writing the code. If you make a small mistake, when you test the page you will receive error indications from the PHP interpreter that are rather obscure, pointing not where the actual error lies, but where it has been firstly detected, and then you will have to look carefully at your script to discover where the mistake is. On another order of things, because the PHP scripting is mixed with the HTML code, any webpage that contains PHP code will provoke errors in HTML validation tools.
When you visit a nice webpage, probably many times have you been curious about lurking at its source code or perhaps tempted to borrow that nice looking design. Well, because PHP is a server-side language, I have bad news for the code-grabbers; you can't access to PHP scripts written in a webpage, since the server does not return the PHP scripting, but only the results of its work. So what PHP is doing all the time is to dynamically write HTML code to change the contents of the webpages, so each time a same webpage (URL) is altered, it will show a different HTML source code. So now, before starting, I will show you a most simple and at the same time useful PHP trick, accessible even for everyone that has never used PHP...
Webpages tend to have elements that repeat in many or every webpages on an entire website. The websites that are written in static HTML will need to update their pages, one by one, when these elements are altered. This can be an overwhelming task, since a website can have hundreds or thousands of webpages sharing that common element that we need to alter. We can avoid this by using a very simple PHP code. Let's see how...
Imagine a typical layout for a webpage, where header and footer are always the same in every page, containing the same details, like links, plain texts or graphics. Well, instead of having the HTML code of these elements inside the webpages where they appear, we cut this code and paste it in an empty text file, and save it with a meaningful name finished with .php extension. So for example, if it stores the header of the website, we probably would name it as header.php. Then, in the same place where the HTML code that we cut was, we will place a instruction like this: <?php include("dyn_layout/header.php"); ?>
Between the double quotes we put the URL where our header file is stored and for finish this task, we save the changes in the webpage and and change its extension to a .php one, so for example, index.htm would become index.php. Changing the extensions will ensure that the PHP scripts will be processed by the server's PHP interpreter. If everything is correctly set, when you visit the modified webpage in the browser after uploading it to the server, you will see that the header is there as usual, and it actually is, since if you look at the webpage's source code, you will see indeed the HTML code of the header and not the PHP script. As I said before, the PHP code is overwritten by its corresponding HTML code and thus not accessible from a client computer.
Even this simple website (my first PHP website) uses that trick to make easier its maintenance towards future changes. And this can be done also with the CSS styling code, so the possibilities are really interesting... Today, millions and millions of websites have PHP scripts installed on them. This is what created the web 2.0, for the good and for the bad. With PHP, the possibility of creating a web 2.0 website is at the fingertips of anyone that has the will to deal with PHP. There is not excuse. As I said, even free hostings support PHP. It will extend greatly the possibilities of your own projects and will allow you to grow as a developer, allowing you to easier achieve a desired position as web developer.
Basic rules for syntax
Source code written in PHP goes enclosed within the tags <?php and ?>. These tags can be used as many times as needed inside a document to place the required paragraphs of PHP code. The PHP interpreter will execute only the commands placed inside the tags. The shortened open tag <? comes from older versions of PHP and its usage is not recommendable nowadays.
Blank spaces, either between lines or between keywords and their values, are ignored by the PHP interpreter, no matter if these are a single space, a tabulation, an entire line or many lines. Therefore, blank spaces can be administered as the programmers decide, having in mind that despite these don't affect the execution of the programs, they affect the readability of the source code.
As a consequence of ignoring blank spaces, the PHP interpreter requires that individual statements are always finished with a semi-colon (;). Lines can be broken into so many lines as the programmer wants, and they will be treated as a single instruction that ends where the first semi-colon is found.
The PHP interpreter is insensitive towards uppercase and lowercase letters in keywords and names of functions. Names of variables are however case-sensitive, so the same name with different case values will be considered to belong to different variables. This feature adds flexibility to naming conventions, but it is not recommended since it is prone to provoke confusions. Names of variables must also begin with a $ symbol followed by a letter or an underscore, while the rest of the characters can be letters, numbers or underscores.
Comments in PHP are preceded by two slashes (//). If the comment occupies more than a line, the slashes have to be included at the start of every line, since comments do not require semi-colons to be finished. These slashes can also be placed after a programming sentence, to start a comment in that very line. Another symbol that can be used instead of // is #, but the first one is generally preferred. If the comment is going to occupy several lines, we can enclose it within the symbols /* and */, not requiring then a // before every line. Remember that comment symbols are useful not only for writing comments, but for temporally or permanently deactivating pieces of source without having to remove them.