Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

Beginning PHP – The Fundamentals

Beginning PHP – The Fundamentals

MAR 10

The Whole Picture

You have an operational web server with PHP installed and configured, and you might be wondering how all the pieces fall together and perform anything useful. Before we move along any further, we would like to share the underlying concept on how PHP and Apache work together; as this will provide you with a solid foundation to understanding the examples used throughout the tutorial.

You already know that PHP code can be inserted directly within the HTML code of a web page, but how does this code get executed? Well, the process is actually quite straight-forward, when someone visits a PHP enabled web page (i.e. index.php) the web server parses the page and when it encounters any PHP code, it invokes the PHP interpreter. The PHP interpreter processes the code, it executes the instructions to perform a particular task, for instance like displaying the current date, accessing a database, performing calculations, or any other number of things. After its done processing, the PHP interpreter replaces the PHP code and translates the output as plain HTML and returns control back to the server. The resulting web page is then served to the person requesting it.

Unlike Javascript or any other client-side scripting language, the source code is not visible. All the instructions are executed on the server and the code is replaced with ordinary HTML, this is a very useful feature of server-side programming languages, as this hides the inner workings of a script and significantly hinders a mischievous person from bypassing security and validation measures on a website.

The Infamous “Hello World” Script

With the background information and some theoretical topics covered, we can start focusing on the practical side of programming. We’ll start by creating a very simple PHP script called hello.php, which will output the phrase “Hello World” to the web browser. This introductory script will demonstrate some basic PHP syntax and will serve as a launch pad for creating powerful applications. Lets get started!

Open Notepad or any other text editor you like, and copy the following lines into a blank file and save it as hello.php in the htdocs subfolder under the Apache installation directory.

<html>
<head>
  <title>Hello World Script</title>

</head>
<body>

<?php
  //this is some php code
  echo '<p>Hello World!</p>';
?>

</body>
</html>

Start Apache, if you haven’t already and load the hello.php script with your favorite web browser. You should get output similar to this:

Hello World!

If you see similar output, then you have successfully created your first PHP script. It might not do much, but it’s a great start!

Embedding PHP

In the preceding example, all the PHP code is between the <?php tag and the closing ?> tag. These delimiting tags are used to separate PHP code from ordinary HTML and trigger the web server to invoke the PHP interpreter. Without properly delimiting PHP code, the web server will treat it as ordinary HTML and display it directly to the browser as text or complain with an error message. Everything between these tags is parsed by the PHP interpreter. If you view the page source, you’ll notice that the PHP code has been replaced with HTML and the output is displayed directly to the browser.

This is the preferred tag style for delimiting PHP code, but there are a few other styles of delimiting PHP tags that can also be used, we’ll look at some of these next.

Short Style

This style is the shortest style to implement, hence its name. You insert all PHP code between an opening <? tag and a closing ?> tag. These tags are not allowed by default, you will need to enable the short_open_tag directive in the Php.ini configuration file, if you want to use this style.

ASP Style

If you have programmed using ASP, you’ll find these tags very handy as they are essentially the same; insert code between an opening <% tag and a %> closing tag. These tags are enabled by the asp_tags setting in the PHP configuration file.

Script Style

This is a standard HTML tag, PHP code is enclosed by <SCRIPT LANGUAGE="php"> and </SCRIPT> tags. You’ll be familiar with this tag style, if you’ve programmed with Javascript or VBscript. This style is enabled by default and no special configuration is required.

You can use any tag style you prefer, just make certain you enclose all your PHP statements within these tags, otherwise your code will not be executed. Similar to HTML, PHP tags are used in pairs with an opening tag and a closing tag, and omitting either one will give you unexpected results.

Comments

As your scripts get longer and more complicated, comments will help keep your code organized and provide details for the functionality of the script. Adding comments throughout your script, that explain the functions and purpose of the code, will help you easily maintain and debug your scripts on future revisions. Comments are ignored by the PHP interpreter, thus they are not executed.

PHP supports both single line and multiple line style comments, similar to other programming languages like C, C++, and Java. The following are examples of both styles:

<?php
// this is a single line comment

# this is another example of a single line comment

/* this is an example of a
multiple line comment */
?>

Multiple line comments can span several lines starting with /* and ending with */. Single line comments are effective until the end of the line or the closing PHP tag, whichever comes first.

In this tutorial, we have introduced a very simple script with limited functionality. The purpose of this example was to focus on key topics and fundamental elements to establish a solid foundation.
We can build upon this foundation and start adding more functionality to our scripts as we explore further into the language. In the next section, we’ll cover outputting from a PHP script and concentrate on variables and constants.

  • Share/Bookmark

You can leave a response, or trackback from your own site.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>