Jump to content

Introduction to PHP scripts


Recommended Posts

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:

<html>

<body>
<?php
// This is a comment
/*
This is a block
of
comments
*/
?>
</body>

</html>

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

Note: 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).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...