Chapter 1: Introduction To PHP
Chapter 1: Introduction To PHP
Chapter 1: Introduction To PHP
iq
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
Why PHP?
• PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).
• PHP is compatible with almost all servers used today (Apache, IIS, etc.).
• PHP supports a wide range of databases.
• PHP is free. Download it from the official PHP resource: www.php.net
• PHP is easy to learn and runs efficiently on the server side.
• PHP is widely-used.
W3Techs https://w3techs.com/
Instructor: Sudad H. Abed / Email: [email protected]
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
As Previously mentioned that PHP is a server-side programming language which means it runs
in the server. PHP plays an intermediate role between a client and the date stored in the server
and other servers.
You can install a server on your personal computer such as XAMPP server from the link below:
https://www.apachefriends.org/index.html
A PHP version and MySQL come out of the box when you install XAMPP server. You can use
other development server instead of XAMPP such as WAMPSERVER that you can download
from here (http://www.wampserver.com/en/).
Instructor: Sudad H. Abed / Email: [email protected]
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
PHP Syntax:
A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
➢ Comments in PHP
A comment in PHP code is a line that is not read/executed as part of the program. Its only
purpose is to be read by someone who is looking at the code.
Ex:
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
➢ PHP Case Sensitivity
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined
functions are NOT case-sensitive.
In the example below, all three echo statements below are legal (and equal):
Instructor: Sudad H. Abed / Email: [email protected]
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
Ex:
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
However; all variable names are case-sensitive.
In the example below, only the first statement will display the value of the $color variable (this
is because $color, $COLOR, and $coLOR are treated as three different variables)
Ex:
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
PHP Variables:
• Start with a $
• Contain only letters, numbers, and the underscore
• The first character after the $ cannot be a number
• Are case-sensitive
• Use a consistent naming scheme!
Ex:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Instructor: Sudad H. Abed / Email: [email protected]
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
A variable declared within a function has a LOCAL SCOPE and can only be accessed within
that function:
Ex:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
Ex:
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
• PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted.
However, sometimes we want a local variable NOT to be deleted. We need it for a
further job.
To do this, use the static keyword when you first declare the variable:
Ex:
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
Then, each time the function is called, that variable will still have the information it
contained from the last time the function was called.
Note: The variable is still local to the function.
PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
Instructor: Sudad H. Abed / Email: [email protected]
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
➢ PHP String
A string can be any text inside quotes. You can use single or double quotes:
Ex:
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
You can use either quotation mark type to create the string.
To use a variable within another string, you must use double quotation marks.
Ex:
$first_name = 'Tobias';
$today = 'August 2, 2011';
$var = "Define \"platitude\", please.";
$var = 'Define "platitude", please.';
echo $first_name;
echo "Hello, $first_name";
➢ PHP Integer
• An integer must have at least one digit
• An integer must not have a decimal point
• An integer can be either positive or negative
• Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based
- prefixed with 0x) or octal (8-based - prefixed with 0)
In the following example $x is an integer. The PHP var_dump() function returns the data
type and value:
Ex:
<?php
$x = 5985;
var_dump($x);
?>
Instructor: Sudad H. Abed / Email: [email protected]
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
➢ PHP Float
float (floating point number) is a number with a decimal point or a number in exponential
form.
In the following example $x is a float. The PHP var_dump() function returns the data type
and value:
Ex:
<?php
$x = 10.365;
var_dump($x);
?>
➢ PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
Ex:
$x = true;
$y = false;
➢ PHP Array
In the following example $cars is an array. The PHP var_dump() function returns the data
type and value:
Ex:
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
➢ PHP Object
An object is a data type which stores data and information on how to process that data.
In PHP, an object must be explicitly declared.
First we must declare a class of object. For this, we use the class keyword. A class is a
structure that can contain properties and methods:
Ex:
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
$herbie = new Car(); // create an object
echo $herbie->model; // show object properties
?>
Instructor: Sudad H. Abed / Email: [email protected]
Lecture Reference: www.w3schools.com ,
PHP and MySQL for Dynamic Web Sites, 4th Edition
Ex:
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
➢ PHP Resource
• The special resource type is not an actual data type. It is the storing of a reference to
functions and resources external to PHP.
• A common example of using the resource data type is a database call.
• We will not talk about the resource type here, since it is an advanced topic