Bookmark this on Delicious
Learn how to write PHP scripts - Introduction
PHP (PHP: Hypertext Preprocessor) is a server-side scripting language which is free and widely used in web development: it allows you to build dynamic websites with the use of dabatases such as MySql databases (which can be queried by using SQL commands from your PHP scripts). PHP is compatible with a large number of servers (e.g. Apache) and platforms (Linux, Windows, Unix), is an open source software, and is very intuitive and easy to learn.
The purpose of this series of tutorials on PHP is to introduce you to the PHP language. Another series of tutorials will later be dedicated to the use of MySql databases and the SQL language. However, you have to keep in mind that only the combination of PHP with the use of databases allows you to create truly dynamic websites. We decided to separate the study of PHP from that of MySQL databases for practical reasons (one of them being that PHP can support many other types of databases, such as Oracle, Access, PostgreSQL, Generic ODBC, but we decided on MyComputerForum to introduce MySQL databases only because they are free, widely used, and a strong community and efficient support has been built around the MySQL solution).
Learn how to write PHP scripts - Running PHP on your server
Before you can develop in PHP, you must be sure that PHP is installed on your server (this will guarantee the execution of the PHP scripts). You can ascertain this by asking your web host, or more simply by looking at the description of your web hosting plan; depending on the situation, for backward compatibility issues, you might also want to check which version of PHP is installed on your server. If you have your own dedicated server, you can install the last version of PHP yourself (it can be downloaded for free on the Internet at php.net).
When developping in PHP, you need to run a local server on your machine in order to check that your scripts function correctly (unless you are so sure about your programming skills that you believe such a verification won't be necessary).
I recommend that you use WAMP, which is a package comprising an Apache server along with the last version of PHP and MySQL; it can be run locally on your computer.
You could also prefer to dowload each component independently (i.e. download PHP at php.net, the Apache server at apache.org, and later on MySql at mysql.com). However, if you do this, you will have to configure your web server yourself in order to establish the communication between all these components.
An alternative is to conduct the tests directly on your web server, but this can be slower and unnecessarily costly, since it will use up bandwith and CPU. Moreover, this can be a problem if you are on a shared server and a programming mistake triggers a CPU overflow (in this case your web host can very well block your account for resource abuse).
Learn how to write PHP scripts - Structure of PHP files and PHP syntax
Certain rules must be respected when programming in PHP:
- PHP scripts can be run only in a PHP file (a file whose extension is .php or .phtml).
- A PHP script always starts with <?php and ends with ?>, while the rest of the PHP file can be structured just like any HTML file. The PHP scripts will be executed on the server and will return HTML to the client's web browser. Please note that it possible to use the lighter syntax <? ... ?> if it is supported by the server. If you are unsure about this, it is better that you use the full PHP syntax.
- A line of PHP code always ends with a semi-colon.
- A line of comments in PHP begins with //; a block of comments begins with /* and ends with */.
Learn the PHP code:
// This is a comment
This is a block
Run the PHP script in your web browser:
Obviously, the output in your web browser will be a blank page, since the file contains only comments ... in the next lesson, we will introduce the php output functions.
PHP scripts can be inserted into HTML code, provided that the file has a .php extension and that the php syntax is followed. The PHP code is executed server-side and returns HTML code to your visitors' browsers (in order to allow the outputs to be displayed). In the following PHP lessons, we will show you how to output text and HTML code in PHP.
Next tutorial: Output functions in PHP (echo, print)
Back to computer forums