PHP Programming Basics

Ryan Dube
php coding

Looking for basic tutorials to introduce you to PHP? The following guide will walk you through the process of understanding the basics about PHP programming.

Getting Started with PHP

The best way to learn PHP is to first understand the basic structure and syntax of the language. Just like any other scripting language, like C, Visual Basic or Java - PHP uses the same basic programming operators and comparators such as if-then statements, while loops, for loops and other standard statements. If you understand basic script programming, then you're already halfway there. If you've never programmed before, then you may want to consider reading through a basic PHP book.

What's Server Side Scripting?

PHP is one of the many Internet programming languages classified as "server side scripting" languages. These are simply programs that reside on your web server and perform all processing at the web server. This means that they can do things that client side scripts (like Javascript) can't do, such as perform queries for information from any database that you have stored on the web server, or dynamically create unique web pages based on which user logs into your website. Using server side scripting languages adds a tremendous amount of power and flexibility to what you can accomplish with your web page. Every web programmer has their own personal favorite scripting language, much of which has to do with what computer platform they prefer, or what language they learned first. Just a few examples of the most common and popular server side scripts include:

  • ASP and ASP.NET
  • ColdFusion
  • Java
  • PHP
  • Perl
  • Ruby
  • Python

PHP Basics: How It Works

The nice feature about PHP is that, much like with ColdFusion, you can easily integrate PHP into a standard HTML page. This integration allows for a dynamic presentation of the web page, because you can make decisions about what HTML to use based on any number of factors, such as the time of day or year or the visitor who just logged in. The simplest example of this integration is displayed below.

<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php echo '<p>This is my first PHP Web Page!</p>'; ?>
</body>
</html>

There are a few things about the above sample code that teach you how PHP works. The first is that, as you can see, this simple page is laid out in standard HTML format. However, anywhere in the page that you like, you can embed any PHP command by using the " " at the end of the statement. When you save this small web page with a ".php" extension, it tells the web server that the page needs to be processed as a PHP file. Assuming you've already had your web host administrator configure the server to accept PHP scripts, then the web server will display this page just as it would any other HTML page that you create.

How to Use PHP Variables and Functions

When you're ready to start doing more dynamic processing, you'll want to start assigning values to PHP variables as your page loads, and then doing something with those values. One example would be, when a user logs into your website, you can have your PHP script look up their address within your user table in the SQL database, and return a customized welcome statement on the page. This sort of script may sound complicated, but it's actually very simple.

<?php

$query = sprintf("SELECT firstname, lastname, city FROM users WHERE firstname='%s' AND lastname='%s''',

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

echo "Welcome ". $row['firstname']; ."! You look stunning today!"<br />\n";

echo "<p>Welcome ".$row['firstname']." I hope the weather in ".$row['city']." is nice today.<br>";

}

?>

You don't really need to understand the commands to follow the logic. The simple steps that the script above performs is as follows.

  1. The mysql_query function checks the user database using a standard SQL statement
  2. The results (the name and city of the user) is stored in the $result variable
  3. The mysql_fetch_assoc function loads the $row array variable with those values
  4. The script dynamically outputs HTML based on the PHP variables

The important thing to understand here is that all variables are preceded with the "$" symbol, and secondly, this ability allows you to perform a variety of powerful tasks that will make your webpage extremely functional and dynamic. You can find a full listing of available PHP functions at php.net.

What are PHP Global Variables?

There are also what are called "predefined variables" that you can use in your PHP script without worrying about loading them with information. These variables can provide your script with real information about the web server, your user's session variables, HTTP cookies and more. You can find a list of these at the reserved variable page on php.net. Some of the most commonly used include:

  • $GLOBALS - These variables can be used anywhere in your PHP scripts
  • $ SERVER - This variables provides important data about the server environment
  • $ SESSION - These variables offer important information about the current user session

Final Words

As all of these examples show, PHP scripts can be as simple or as complicated as you want them to be. The ability to integrate PHP within the HTML pages that you're accustomed to writing makes it very easy to learn how PHP behaves, and how you can use it to easily make your page far more interesting and dynamic.

PHP Programming Basics