Database Programming :: Lessons :: PHP
Structure of Web Services
A web browser is used to format and display web pages. A web server sends web pages to the browser and responds to requests from visitors to the website. A database server is a type of information server and responds to queries for information from its database.

HTTP is the protocol used to allow browsers and servers to communicate. A web server responds to requests for web pages and returns a new web page to the browser. Server-side languages such as PHP are programming languages that reside on the server to process requests. Server-side languages can be used to process more complicated requests and serve dynamic results.
PHP Basics
PHP was designed to help developers create dynamic and data-driven websites. It is a server-side language, therefore, you must upload your PHP pages to the server or install PHP on your local computer to use it. You can embed PHP in HTML documents as long as they have the .php extension. HTML can also be embedded in PHP scripts. Two great resources for learning about PHP are the following:
- PHP.net: The official documentation site for PHP maintained by the PHP Group.
- W3Schools' PHP Tutorial: A fantastic walkthrough for learning PHP.
Like most languages, all statements in PHP end with a semicolon, unless that statement begins and ends with curly brackets { }. The echo function can be used to output text (and HTML) from a PHP script.
<?php echo("Hello world"); ?>
The <?php and ?> tags are used to denote the begin and end of PHP code. They are required in every PHP script and whenever you embed PHP in an HTML document. If you create the above script and upload it to your web server, navigating to the page will simply display the text "Hello world." Viewing the source code will not reveal the underlying PHP code. Below is the same example, but embeding the PHP code in an HTML document:
<html> <head> <title>PHP Embedded in HTML</title> </head> <body> <p> <?php echo("Hello world"); ?> </p> </body> </html>
PHP Variables
All variables in PHP are preceded by a dollar sign ($) and follow typical programming language naming conventions meaning variable names can contain letters, numbers, and a limited subset of symbols such as - and _. PHP is a loosely-typed language, which means variables do not have to be declared ahead of time.
If a variable is defined at the beginning of a PHP file it remains in memory until the end of the file. If a variable is assigned a value in a file that calles a second PHP file, and the second PHP file has a variable of the same name, the second variable will replace the value of the first variable.
Superglobals are predefined variables in PHP and available in all scripts. The following are some of the superglobals available in PHP:
- $_GET contains any variables provided to the script using the GET method.
- $_POST contains any variables provided to the script using the POST method.
- $_COOKIE contains any variables provides to the script through cookies.
- $_FILES contains information provided to the script through an uploaded file.
- $_SERVER contains information about the web server that hosts the PHP script.
- $_REQUEST contains any variables provided to the script through GET, POST, or cookies.
- $_SESSION contains any variables provided to the script through a session.
To define a constant in PHP you can use PHP's define() function. You do not use a $ when accessing a constant.
<?php define("SCHOOL", "Yorkville High School"); $mySchool = $_POST['school']; if ($mySchool == constant("SCHOOL")) echo("Go Foxes!"); ?>
Java vs PHP
Java and PHP have many similarities. The following are exactly the same in PHP and Java:
- If statements
- For loops
- While loops
- Variable operators (+, -, *, /, %)
- Comparision operators other than equality (!=, <, >, <=, >=)
- Logical operators (!, &&, ||)
- Comments
Equality in PHP can be done using ==, but == does not compare variable types. This can lead to some hard to diagnose errors. When comparing data in PHP that comes from outside the script it is best to use ===, which compares value as well as variable type.
Arrays in PHP
Arrays work similarly in PHP as they do in Java. You can define an array like the following:
<?php $pokemonStarters = array("Bulbasaur", "Charmander", "Pikachu", "Squirtle"); echo($pokemonStarters[2]); //Outputs "Pikachu" ?>
An associative array stores data in key/value pairs like the following:
<?php $pokemonStarters = array("Bulbasaur"=>"Grass/Poison", "Charmander"=>"Fire", "Pikachu"=>"Electric", "Squirtle"=>"Water"); echo($pokemonStarters["Bulbasaur"]); //Outputs "Grass/Poison" ?>
The count() function can be used to return the number of elements in an array. There are nearly 60 functions you can use with arrays. Arrays are a pivotal part of PHP since all of the data you receive from outside PHP scripts is sent via superglobals, which are stored as arrays.