Section 0: Module Objectives or Competencies
Course Objective or Competency |
---|
The student will be able to write basic PHP applications in order to connect to and manipulate a database using PHP. |
Section 1: Overview
Recall that we said that dynamic and e-commerce websites use databases to store the content that is loaded into a web page when the page is requested by the user.
- Sites like Amazon dynamically generate page content from the data in a database.
The Interactive Web Development course teaches how to use some of the features of JavaScript to make web pages interactive.
However, JavaScript is primarily a client-side language and therefore is not the best choice for dynamically-generated content.
This class will introduce you to one of the most widely used server-side languages, PHP.
Here is a thorough "Introduction to PHP".
Section 2: PH what?
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
PHP originally stood for Personal Home Page, but now represents the recursive acronym PHP: Hypertext Preprocessor.
PHP is a server-side language.
-
A server-side language is similar to JavaScript in that it allows you to embed brief
programs (scripts) into the HTML code of a web page.
- When executed, these scripts provide greater control over what appears in the browser window than HTML alone can provide.
The key difference between JavaScript and PHP is the stage of loading the web page at which these embedded programs are executed.
- Client-side languages like JavaScript are read and executed by the web browser, after downloading the web page (embedded programs and all) from the web server.
- Server-side languages like PHP are run by the web server, before sending the web page to the browser
Whereas client-side languages give you control over how a page behaves once it's displayed by the browser, server-side languages let you generate customized pages on the fly before they’re even sent to the browser.
- Once the web server has executed the PHP code embedded in a web page, the results of that code’s execution take the place of the PHP code in the page.
When the browser receives the page, all it sees is standard HTML code, hence the name – server-side language.

How the Internet Works
Section 3: PHP vs. JavaScript or PHP plus JavaScript?
All browsers support the output of PHP code. However, due to security concerns some people disable JavaScript in their browsers unless they are visiting a fully trusted site.
It is quite common to use them both, especially when site content is stored in a database.
- JavaScript is used for the interactive client-side features.
- PHP is used to generate the page contents.
Since PHP pages work well on all platforms, a particular strength is that it can be used to develop websites on a desktop system and then deployed on industrial-strength and secure servers.
JavaScript vs. PHP
-
similarities
- both are interpreted, not compiled
- both are relaxed about syntax, rules, and types
- both are case-sensitive
- both have built-in regular expressions for powerful text processing
-
differences
- JavaScript is a client-side language that resides in your browser and is available for use with no effort on your part; PHP must be installed on a server (local or remote), and you must have specific permissions to access PHP applications.
- JavaScript cannot access any information outside an HTML document; PHP applications can access and create files stored elsewhere in a server.
- JavaScript is more object-oriented: noun.verb(), less procedural: verb(noun).
- JavaScript focuses on user interfaces and interacting with a document; PHP is geared toward HTML output and file/form processing.
- JavaScript code runs on the client's browser; PHP code runs on the web server.
Benefits of each...
-
JavaScript:
- usability: can modify a page without having to post back to the server (faster User Interface)
- efficiency: can make small, quick changes to page without waiting for server
- event-driven: can respond to user actions like clicks and key presses
-
PHP:
- security: has access to server's private data; client can't see source code
- compatibility: not subject to browser compatibility issue
- power: can write files, open connections to servers, connect to databases, etc.
More comparisons here.
Section 4: Example
As noted, PHP is embedded in your HTML code, with a .php extension rather than .htm or .html.
Let’s look back at a simple example (today1.php / today1.php.txt):
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Today's Date</title>
</head>
<body>
<p>Today's date (according to this web server) is <br/>
<?php
date_default_timezone_set('America/Denver');
echo date('l, F dS Y.');
?>
</p>
</body>
</html>
Most of this is plain HTML; however, the line between <?php and ?> is PHP code.
- <?php marks the start of an embedded PHP script and ?> marks the end of such a script.
- The web server is asked to interpret everything between these two delimiters, and to convert it to regular HTML code before it sends the web page to the requesting browser.
Look at the page source using View | Page Source. Notice that all signs of the PHP code have disappeared; in its place, the output of the script has appeared, and it looks just like standard HTML.
This example demonstrates several advantages of server-side scripting:
-
No browser compatibility issues
- PHP scripts are interpreted on the web server, so there’s no need to worry about whether the language you’re using is supported by the visitor’s browser.
-
Access to server-side resources
- If the date had been inserted using JavaScript, the page would only be able to display the date according to the computer on which the web browser was running.
- While that isn't impressive, access to server-side resources means that PHP can be used to insert content pulled from a MySQL database.
-
Reduced load on the client
- JavaScript can significantly delay the display of a web page on slower computers, because the browser must run the script before it can display the web page.
- With server-side code, this burden is passed to the web server machine, which are generally more powerful machines.
-
Client security
- Whenever you allow a JavaScript to run on your machine you are placing your trust in the developer. While JavaScript is generally safe, bad things can still happen.
- Server-side code helps to insulate the client machine.