CMPF123 - Chapter 5
Chapter Overview
Title: PHP: Hypertext Preprocessor
Course: CMPF 123: Introduction to Web Development
Learning Outcomes
At the end of this lesson, students should be able to:
Understand basic PHP concepts
Perform basic operations using PHP
Apply PHP codes in HTML pages where appropriate
Topics Covered
Introduction to PHP
Strings
Installation of PHP
Constants
Syntax
Operators
Variables
Conditional Statements
Echo/Print
Loops
Data Types
PHP Basics
What is PHP?
Definition: PHP stands for "PHP: Hypertext Preprocessor".
Type: PHP is a server scripting language and a powerful tool for creating dynamic and interactive web pages.
Comparison: It is a widely-used, free, efficient alternative to Microsoft ASP.
Understanding PHP Files
Knowledge Requirement: Students should have a basic understanding of HTML, CSS, and JavaScript.
Content: PHP files can contain text, HTML, CSS, JavaScript, and PHP code.
Execution: PHP code is executed on the web server, and results are returned as plain HTML.
File Extension: PHP files have the extension ".php".
PHP Functionalities
Can:
Generate dynamic page content
Handle files on the server (create, open, read, write, delete)
Collect form data
Manage cookies
Manipulate database data
Control user access
Encrypt data
Requirements to Start with PHP
Option 1: Find a web host that supports PHP.
Option 2: Install a web server on your PC and then PHP.
Example Tools:
WampServer: A web development platform for Windows.
XAMPP: Provides Apache server, MySQL, PHP, and Perl for multiple operating systems.
Creating and Executing PHP Programs
Initial Setup
Open a code editor.
Type the code:
<?php phpinfo(); ?>Save the file as
phpinfo.phpin the web server’s root directory (e.g.,wamp/www).
Test the Setup
In your web browser, navigate to:
http://localhost/phpinfo.php. If successful, congratulations!
PHP Syntax
A PHP script is executed on the server, and the resulting HTML is sent back to the browser.
PHP scripts begin with
<?phpand end with?>.
Basic PHP Program Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php echo "Testing 123"; ?>
</body>
</html>PHP Comments
Used to:
Explain code to others.
Remind oneself of code functionality.
Case Sensitivity in PHP
Keywords (if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.
Variable names are case-sensitive.
PHP Variables
Variables start with a
$sign.Examples:
<?php $txt = "Hello world!"; $x = 5; cars = array("Volvo", "BMW", "Toyota");PHP Constants
Define constant values that cannot change:
define(name, value, case-insensitive).
PHP Operators
Arithmetic Operators
Operator
Description
Example
Result
+
Addition
$x + $y
Sum of $x and $y
-
Subtraction
$x - $y
Difference of $x and $y
*
Multiplication
$x * $y
Product of $x and $y
/
Division
$x / $y
Quotient of $x and $y
%
Modulus
$x % $y
Remainder of $x / $y
**
Exponentiation
$x ** $y
$x raised to the power of $y
Comparison Operators
Operator
Description
Example
Result
==
Equal
$x == $y
True if $x equals $y
===
Identical
$x === $y
True if both value and type are equal
!=
Not Equal
$x != $y
True if $x is not equal to $y
Conditional Statements
Types
if: Executes code if a condition is true.
if...else: Executes different code based on conditions.
if...elseif...else: Handles multiple conditional checks.
switch: Selects from many blocks of code to be executed based on variable value.
PHP Loops
while: Executes block while condition is true.
do while: Executes at least once and continues while condition is true.
for: Loops for a specified number of iterations.
foreach: Loops through each element of an array.
Thank You!
Expressing gratitude in various languages.
Example phrases included: "Gracias", "Arigato", "Merci", etc.