PHP Development Lecture Notes
Server-side Development with PHP
Introduction to PHP
PHP is a server-side scripting language specifically designed for the web.
It allows for embedding PHP code within an HTML page.
PHP code is executed at the web server, generating HTML or other output for the visitor to see.
Origin of PHP
Originally stood for Personal Home Page.
Name changed according to the GNU recursive naming convention, now stands for PHP Hypertext Preprocessor.
Strengths of PHP
Main Competitors
Major competitors include:
Python
Ruby (including Ruby on Rails)
Node.js
Perl
Microsoft .NET
Java
Key Strengths
Performance: Efficient processing of scripts.
Scalability: Suitable for growth as demands increase.
Database Interfaces: Supports many different database systems.
Built-in Libraries: Offers many libraries for common web tasks.
Low Cost: Generally cost-effective to use.
Ease of Learning: User-friendly syntax conducive to beginners.
Object-Oriented Support: Strong support for object-oriented programming concepts.
Portability: Runs on various platforms with little modification.
Development Flexibility: Offers a variety of approaches to development.
Source Code Availability: Open-source nature allows for modification and distribution.
Support and Documentation: Extensive support and resources are available.
Incorporating PHP within HTML
File Extension
PHP documents typically end with the .php extension.
Web servers recognize this extension and automatically pass it to the PHP processor.
Configuration Options
Developers can configure web servers to process files with .htm or .html extensions through the PHP processor to hide the use of PHP.
Basic PHP Document Structure
A basic PHP document that outputs only HTML might look as follows:
<?php
// your PHP code here
?>
An existing HTML file (e.g.,
index.html) can be renamed toindex.phpand it will display identically but can also incorporate PHP.
PHP Command Syntax
PHP commands are defined within tags starting with and ending with ?>.
Using Comments in PHP
Single-line Comments
Use // to comment a single line.
Multi-line Comments
Use / and */ for comments across multiple lines:
/* This is a
multi-line comment */
Error Handling
A common error in PHP is forgetting to end commands with a ; (semicolon), leading to a Parse error because PHP treats multiple statements as a single statement.
Variables in PHP
Variable Declaration
All variables in PHP must start with a $ sign (e.g., $variable).
This convention helps the PHP parser identify variables quickly.
Super Global Variables
Definition
Super global variables are predefined variables accessible across all scopes within PHP.
Common Super Global Variables
_POST: Collects data from an HTML form after submission.
_FILES: A multidimensional array containing information about uploaded files.
$_SESSION: Stores session data that persists during a user's session.
Arrays in PHP
Basic Definition
An array stores multiple values in a single variable.
Indexed Arrays
Arrays can have numeric indices. Example:
$paper[] = "Copier";
$paper[] = "Inkjet";
Output example:
Array(
[0] => "Copier"
[1] => "Inkjet"
...
)
Two-dimensional and Multi-dimensional Arrays
Definition
A two-dimensional array is defined as an array of arrays; a three-dimensional array is an array of arrays of arrays.
Example
A tic-tac-toe game uses a structure of nine cells arranged in a 3x3 format.
Associative Arrays
Definition
An associative array allows elements to be referenced by names instead of indices:
$paper['copier'] = "Copier & Multipurpose";
Operators in PHP
Arithmetic Operators
Common operators include:
+: Addition
-: Subtraction
**
**: Multiplication/: Division
%: Modulus
Assignment Operators
Examples include:
=: Simple assignment
+=: Adds and assigns ($j += 5 is equivalent to $j = $j + 5)
-=: Subtracts and assigns
Comparison Operators
Used for comparing two values. Examples include:
==: Equal to
!=: Not equal to
>: Greater than
<: Less than
>=: Greater than or equal to
<=: Less than or equal to
Logical Operators
Combine conditional statements using:
&&: And
||: Or
!: Not
Conditional Statements
If Statement
The structure can evaluate any PHP expression, such as equality and comparison.
Else Statement
Provides alternative code to execute when the if condition is not TRUE.
Elseif Statement
Allows for multiple conditions using elseif for a sequence of possibilities.
Switch Statement
Useful for scenarios where a variable might have multiple potential values, each triggering distinct actions.
Loops in PHP
Purpose
Loops are used to repeat a sequence of code until a certain condition is met (e.g., user input).
Loop Types
While loop: Continues as long as a condition is TRUE.
Do while loop: Executes at least once before checking the condition.
For loop: Generally used for iterating a specific number of times.
Foreach … as loop: Specialized for iterating over arrays.
Functions in PHP
Built-in Functions
PHP offers many built-in functions for various operations.
Defining a Function
Function names are case-insensitive.
A function's structure includes:
function functionName() {
// code to execute
}
Each function can include one or more return statements to send values back to the caller.
Including and Requiring Files
Purpose
Facilitates code organization; reuse functions without duplicating code.
Commands
include: Pulls in the contents of a file.
include_once: Ensures that a file is included only once during the execution.
require: Similar to include, but halts execution if the file is not found.
require_once: Like require but done only once, ensuring the code is not added multiple times.