Lecture 20
Lecture 20
Introduction to PHP
Today’s Lecture
Introduction to PHP
• PHP Introduction
• PHP Variables
• PHP Constants
• PHP Data Types
• PHP Conditional Statements
• PHP Loops
• PHP Functions
PHP Introduction
• PHP is an acronym for “PHP: Hypertext Preprocessor”
– Initially known as Personal Home Page.
• Originally created by Rasmus Lerdorf in 1994.
• PHP is a server-side scripting language, and a powerful tool for
making dynamic and interactive web pages.
• PHP is widely-used, and an open-source scripting language.
– Open-source content management systems like WordPress is
written in PHP.
• PHP scripts are executed on the server.
• PHP is free to download and use.
• If anyone have basic knowledge of HTML, CSS, and JavaScript; then
he/she can code PHP.
• PHP 8.3 (released on 23rd Nov 2023) is the latest stable release.
PHP Introduction
PHP File
• PHP files can contain text, HTML, CSS, JavaScript, and PHP code.
• PHP code is executed on server, and result is returned to the
browser as plain HTML.
• PHP files have extension ".php"
Setting Up Local Web Server
• Installation
– Install a web server
– Install PHP
– Install a database
• We can either install them individually or choose a pre-configured
package for our Operating System.
• Popular pre-configured packages are XAMPP and WampServer.
PHP Introduction
PHP Syntax
• PHP script is executed on the server, and the plain HTML result is sent
back to the browser.
• PHP script can be placed anywhere in the document.
• PHP script starts with <?php and ends with ?> tag.
<?php
//PHP code goes here
?>
– <?php and ?> simply tells the code block of the PHP code.
• PHP statements, function declarations, etc. appear between
these endpoints/tags.
– Every PHP statement ends with a semicolon ;
• Semicolon ; tells the end of current statement.
• Example: write PHP script to display text "Hello World!" on a web page:
<?php
echo "Hello World!";
?>
PHP Introduction
PHP Case Sensitivity
• In PHP, keywords (example: if, else, while, echo, etc.), classes,
functions, and user-defined functions are not case-sensitive.
• Example: all three echo statements below are equal and legal.
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
• However, all variable names are case-sensitive.
– Example: $color, $COLOR, and $coLOR are treated as three
different variables
PHP Variables
• Variables are “containers” for storing information, like string of text,
numbers, etc.
• In PHP, variable starts with Dollar sign $, followed by name of
variable.
• In PHP, variable doesn’t need to be declared before adding a value
to it.
– PHP automatically converts variable to correct data type,
depending on its value.
– After declaring a variable, it can be reused throughout the code.
• Assignment operator = is used to assign value to a variable.
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
PHP Variables
Rules for PHP Variables
• A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
• A variable starts with Dollar sign $, followed by name of variable.
• A variable name must start with a letter or underscore character.
• A variable name can’t start with a number.
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ ).
• Variable names are case-sensitive ($age and $AGE are two different
variables)
PHP Constants
• Constant is an identifier (name) for a simple value, and value can’t be
changed during the script.
• A valid constant name starts with a letter or underscore (no Dollar
sign $ before constant name).
• Constants are automatically global across the entire script.
• Constants are defined using PHP's define() function
• Syntax: define(name, value, case-insensitive)
– name: specifies name of the constant
– value: specifies value of the constant
– case-insensitive: specifies whether constant name should be case-
insensitive (default is false)
<?php
define("greeting", "Welcome to CSC336 Web Technologies...!");
echo greeting;
?>
PHP Data Types
• Variables can store data of different types.
• PHP supports the following data types:
– String
– Integer
– Float
– Boolean
– Array
– Object
– NULL
• These data types are used to construct variables.
PHP Data Types
PHP String
• String is a sequence of characters, for example "Hello world!".
• String can be any text inside quotes, either single or double quotes.
<?php
$x = "CSC336 Web Technologies...!";
$y = 'CSC336 Web Technologies...!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Data Types
PHP Integer
• Integer data type is a non-decimal number.
<?php
$x = 5985;
echo $x;
?>
<?php
$x = 5985;
var_dump($x);
?>
PHP Boolean
• Boolean represents two possible states: True and False.
• Boolean are often used in conditional testing.
$x = true;
$y = false;
PHP Data Types
PHP Array
• Array stores multiple values in one single variable.
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Data Types
PHP Object
• A class is a template for objects, and an <?php
object is an instance of a class. class Car {
• When individual objects are created, they public $color;
public $model;
inherit all the properties and behaviors from
public function __construct($color,
the class, but each object will have different
$model) {
values for the properties.
$this->color = $color;
• Example: we have a class named Car. A Car $this->model = $model;
can have properties like model, color, etc. }
– We can define variables like $model, public function message() {
$color, and so on, to hold the values of return "My car is a " . $this-
these properties. >color . " " . $this->model . "!";
– When the individual objects (Volvo, }
BMW, Toyota, etc.) are created, they }
inherit all the properties and behaviors $myCar = new Car("White", "Volvo");
from the class, but each object will have echo $myCar -> message();
different values for the properties. echo "<br><br>";
$myCar = new Car("Red", "Toyota");
– If we create a __construct() function,
echo $myCar -> message();
PHP will automatically call this function
?>
when we create an object from a class.
PHP Data Types
PHP NULL Value
• Null is a special data type which can have only one value: NULL
• If a variable is created without a value, it is automatically assigned a
value of NULL.
• Variables can also be empty by setting the value to NULL.
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
PHP Conditional Statements
Conditional statements are used to perform different actions based on
different conditions.
• if
– It executes some code if one condition is true.
– Syntax
if (condition) {
code to be executed if condition is true;
}
<?php
$t = date("H");
if ($t < "20") { Have a good day
echo "Have a good day";
}
?>
PHP Conditional Statements
• if...else
– It executes some code if a condition is true, and another code if
that condition is false.
– Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day"; Have a good day
} else {
echo "Have a good night";
}
?>
PHP Conditional Statements
• if...elseif...else
– It executes different codes for more than two conditions.
– Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is
true;
} else {
code to be executed if all conditions are false;
}
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning";
} elseif ($t < "20") { Have a good day
echo "Have a good day";
} else {
echo "Have a good night";
}
?>
PHP Conditional Statements
• switch
– It selects one of many blocks of code to be executed.
– Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break; <?php
$favcolor = "red";
case label2: switch ($favcolor) {
code to be executed if n=label2; case "red":
break; echo "Your favorite color is red!";
break;
case label3: case "blue":
code to be executed if n=label3; echo "Your favorite color is blue!";
break;
break; case "green":
... echo "Your favorite color is green!";
default: break;
default:
code to be executed if n is different from all labels; echo "Your favorite color is neither
} red, blue, nor green!";
}?>
PHP Loops
Loops are used to execute the same block of code again and again, as
long as a certain condition is met.
• While
– It through a block of code as long as the condition specified
evaluates to true.
– Syntax
while (condition is true) {
code to be executed;
}
<?php
The number is: 1
$x = 1;
The number is: 2
while($x <= 5) {
The number is: 3
echo "The number is: $x <br>";
The number is: 4
$x++;
The number is: 5
}
?>
PHP Loops
• do…while
– It block of code executed once and then condition is evaluated.
• If condition is true, statement is repeated as long as the
specified condition is true.
– Syntax
do {
code to be executed;
} while (condition is true);
<?php
$x = 1;
do { The number is: 1
echo "The number is: $x <br>"; The number is: 2
$x++; The number is: 3
} while ($x <= 5); The number is: 4
?> The number is: 5
PHP Loops
• for
– It through a block of code until the counter reaches a specified
number.
– Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
• init counter: initialize the loop counter value.
• test counter: evaluated for each loop iteration. If it evaluates to TRUE, the loop
continues. If it evaluates to FALSE, the loop ends.
• increment counter: increases the loop counter value.
The number is: 1
<?php
for ($x = 1; $x <= 5; $x++) {
The number is: 2
echo "The number is: $x <br>";
}
The number is: 3
?>
The number is: 4
<?php Red
$colors = array("Red", “Green", “Blue", “Yellow"); Green
foreach ($colors as $value) { Blue
echo "$value <br>"; Yellow
}
?>
PHP Functions
• A function is a self-contained block of code that performs a specific
task.
• PHP Built-in Functions
– PHP has more than 1000’s built-in functions that can be called
directly, from within a script to perform a specific task.
• PHP User-Defined Functions
– Besides built-in PHP functions, it’s possible to create own
functions.
<?php
function writeMsg() {
echo "Hello world!";
} Hello world!
writeMsg();
?>
Summary of Today’s Lecture
Introduction to PHP
• PHP Introduction
• PHP Variables
• PHP Constants
• PHP Data Types
• PHP Conditional Statements
• PHP Loops
• PHP Functions
References
• https://www.php.net/manual/en/
• http://php.net/manual/en/install.php
• https://www.w3schools.com/php/default.asp
• https://www.tutorialrepublic.com/php-tutorial/
• https://www.tutorialspoint.com/php/index.htm