You are on page 1of 13

Learning Guide 1 – Advance Web Technology

Information Sheet 1 Have a good understanding on Advance Web Tech.

Brief History of PHP

PHP development began in 1994 when the developer Rasmus Lerdorf wrote a series of
Common Gateway Interface (CGI) Perl scripts, which he used to maintain his personal
homepage. The tools performed tasks such as displaying his résumé and recording his web
traffic. He rewrote these scripts in C for performance reasons, extending them to add the
ability to work with web forms and to communicate with databases, and called this
implementation "Personal Home Page/Forms Interpreter" or PHP/FI.

PHP/FI could be used to build simple, dynamic web applications. Lerdorf initially announced
the release of PHP/FI as "Personal Home Page Tools (PHP Tools) version 1.0" publicly to
accelerate bug location and improve the code, on the Usenet discussion group
comp.infosystems.www.authoring.cgi on June 8, 1995. This release already had the basic
functionality that PHP has as of 2013. This included Perl-like variables, form handling, and the
ability to embed HTML. The syntax resembled that of Perl but was simpler, more limited and
less consistent.

Overview of PHP Structure and Syntax

PHP programs are written using a text editor, such as Notepad, Simple Text, or any
Web Development Tool, just like HTML pages. However, unlike HTML, PHP files end with a .php
extension. This extension signifies to the server that it needs to parse the PHP code before
sending the resulting HTML code to the viewer ’ s web browser.
PHP is a server scripting language, and is a powerful tool for making dynamic and
interactive Web pages quickly.
PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's
ASP.

What is PHP?
- PHP is an acronym for "PHP Hypertext Preprocessor"
- PHP is a widely-used, open source scripting language
- PHP scripts are executed on the server
- PHP costs nothing, it is free to download and use.

What is a PHP File?


- PHP files can contain text, HTML, CSS, JavaScript, and PHP code
- PHP code are executed on the server, and the result is returned to the browser as
plain HTML
- PHP files have extension ".php"

What Can PHP Do?


- PHP can generate dynamic page content
- PHP can create, open, read, write, delete, and close files on the server
- PHP can collect form data
- PHP can send and receive cookies
- PHP can add, delete, modify data in your database
- PHP can restrict users to access some pages on your website
- PHP can encrypt data

* With PHP you are not limited to output HTML. You can output images, PDF files, and
even Flash movies. You can also output any text, such as XHTML and XML.

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 1
Learning Guide 1 – Advance Web Technology
Information Sheet 1 Have a good understanding on Advance Web Tech.

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 (open source).
- PHP is easy to learn and runs efficiently on the server side

To start using PHP, you can:


- install a web server
- install PHP
- install a database, such as MySQL

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 2
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

Basic PHP Syntax

A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>

<?php
// PHP code goes here
?>

The default file extension for PHP files is ".php".

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in
PHP function "echo" to output the text "Hello World!" on a web page:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>First Activity</title>
</head>
<body>
<?php
echo 'My First PHP File';
?>
</body>
</html>

Note: PHP statements are terminated by semicolon (;). The closing tag of a block of
PHP code also automatically implies a semicolon (so you do not have to have a semicolon
terminating the last line of a PHP block).

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 editing the code!

Comments are useful for:

- To let others understand what you are doing - Comments let other programmers
understand what you were doing in each step (if you work in a group)
- To remind yourself what you did - Most programmers have experienced coming back
to their own work a year or two later and having to re-figure out what they did.
Comments can remind you of what you were thinking when you wrote the code

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 1
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

PHP supports three ways of commenting:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>First Activity</title>
</head>
<body>
<?php

// This is a single line comment

# This is also a single line comment

/*
This is a multiple lines comment block
that spans over more than
one line
*/

echo 'My First PHP File';


?>
</body>
</html>

PHP Case Sensitivity


In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo,
etc.) are NOT case-sensitive.

In the example below, all three echo statements below are legal (and equal):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>First Activity</title>
</head>
<body>
<?php
echo 'My First PHP File';
ECHO 'My First PHP File';
EcHo 'My First PHP File';
?>
</body>
</html>

However; in PHP, all variables are case-sensitive.

In the example below, only the first statement will display the value of the $department
variable (this is because $department, $DEPARTMENT, and $dePARTMENT are treated as
three different variables):

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 2
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

<!DOCTYPE html>
<html>
<body>
<?php
$department="ICT";
echo "My car is " . $department . "<br>";
echo "My house is " . $DEPARTMENT . "<br>";
echo "My boat is " . $dePARTMENT . "<br>";
?>
</body>
</html>

PHP Variables
- Variables are "containers" for storing information
- As with algebra, PHP variables can be used to hold values (x=5) or expressions
(z=x+y).

- A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).

Rules for PHP variables:


- A variable starts with the $ sign, followed by the name of the variable
- A variable name must start with a letter or the underscore character
- A variable name cannot 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 ($y and $Y are two different variables)

PHP Data Types


- A data type is a classification of the type of data that a variable or object can hold in
computer programming. Data type is an important factor in virtually all computer
programming languages.

PHP Strings
A string is a sequence of characters, like "Hello world!".

A string can be any text inside quotes. You can use single or double quotes:

Example

<?php
$x = "Hello world!";
echo $x;
echo "<br>";
$x = 'Hello world!';
echo $x;
?>

PHP Integers
- An integer is a number without decimals.

Rules for integers:


- An integer must have at least one digit (0-9)
- An integer cannot contain comma or blanks
- An integer must not have a decimal point
Course Title: Author: Irwin C. Cansejo, MIT
Advanced Web Technology October 2016 Page 3
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

- 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 we will test different numbers. The PHP var_dump() function returns
the data type and value of variables:

Example

<?php
$x = 5985;
var_dump($x);
echo "<br>";
$x = -345; // negative number
var_dump($x);
echo "<br>";
$x = 0x8C; // hexadecimal number
var_dump($x);
echo "<br>";
$x = 047; // octal number
var_dump($x);
?>

PHP Floating Point Numbers


- A floating point number is a number with a decimal point or a number in exponential
form.

In the following example we will test different numbers. The PHP var_dump() function returns
the data type and value of variables:

Example

<?php
$x = 10.365;
var_dump($x);
echo "<br>";
$x = 2.4e3;
var_dump($x);
echo "<br>";
$x = 8E-5;
var_dump($x);
?>

PHP Booleans
- Booleans can be either TRUE or FALSE.

$x=true;
$y=false;

Booleans are often used in conditional testing. You will learn more about conditional testing
in a later chapter of this tutorial.

PHP Arrays
- An array stores multiple values in one single variable.

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 4
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

In the following example we create an array, and then use the PHP var_dump() function to
return the data type and value of the array:

Example

<?php
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
?>

PHP Operators

PHP Arithmetic Operators

Operator Name 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 divided by $y

The example below shows the different results of using the different arithmetic operators:

Example
<?php
$x=10;
$y=6;
echo ($x + $y); // outputs 16
echo ($x - $y); // outputs 4
echo ($x * $y); // outputs 60
echo ($x / $y); // outputs 1.6666666666667
echo ($x % $y); // outputs 4
?>
PHP Assignment Operators
- The PHP assignment operators is used to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the
value of the assignment expression on the right.

Assignment Same as... Description

x=y x=y The left operand gets set to the value of the
expression on the right

x += y x=x+y Addition

x -= y x=x-y Subtraction

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 5
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

x *= y x=x*y Multiplication

x /= y x=x/y Division

x %= y x=x%y Modulus

The example below shows the different results of using the different assignment operators:

Example
<?php
$x=10;
echo $x; // outputs 10

$y=20;
$y += 100;
echo $y; // outputs 120

$z=50;
$z -= 25;
echo $z; // outputs 25

$i=5;
$i *= 6;
echo $i; // outputs 30

$j=10;
$j /= 5;
echo $j; // outputs 2

$k=15;
$k %= 4;
echo $k; // outputs 3
?>
PHP String Operators

Operator Name Example Result

. Concatenation $txt1 = "Hello" Now $txt2


$txt2 = $txt1 . " world!" contains
"Hello world!"

.= Concatenation assignment $txt1 = "Hello" Now $txt1


$txt1 .= " world!" contains
"Hello world!"

The example below shows the results of using the string operators:

Example
<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world!

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 6
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world!
?>

PHP Increment / Decrement Operators

Operator Name Description

++$x Pre-increment Increments $x by one, then returns $x

$x++ Post-increment Returns $x, then increments $x by one

--$x Pre-decrement Decrements $x by one, then returns $x

$x-- Post-decrement Returns $x, then decrements $x by one

The example below shows the different results of using the different increment/decrement
operators:

Example
<?php
$x=10;
echo ++$x; // outputs 11

$y=10;
echo $y++; // outputs 10

$z=5;
echo --$z; // outputs 4

$i=5;
echo $i--; // outputs 5
?>

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result

== Equal $x == $y True if $x is equal to


$y

=== Identical $x === $y True if $x is equal to


$y, and they are of
the same type

!= Not equal $x != $y True if $x is not equal


to $y

<> Not equal $x <> $y True if $x is not equal

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 7
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

to $y

!== Not identical $x !== $y True if $x is not equal


to $y, or they are not
of the same type

> Greater than $x > $y True if $x is greater


than $y

< Less than $x < $y True if $x is less than


$y

>= Greater than or equal to $x >= $y True if $x is greater


than or equal to $y

<= Less than or equal to $x <= $y True if $x is less than or


equal to $y

The example below shows the different results of using some of the comparison operators:

Example
<?php
$x=100;
$y="100";

var_dump($x == $y);
echo "<br>";

var_dump($x === $y);


echo "<br>";

var_dump($x != $y);
echo "<br>";

var_dump($x !== $y);


echo "<br>";

$a=50;
$b=90;

var_dump($a > $b);


echo "<br>";

var_dump($a < $b);


?>

PHP Logical Operators

Operator Name Example Result

And And $x and $y True if both $x and $y are true

Or Or $x or $y True if either $x or $y is true

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 8
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

Xor Xor $x xor $y True if either $x or $y is true, but not both

&& And $x && $y True if both $x and $y are true

|| Or $x || $y True if either $x or $y is true

! Not !$x True if $x is not true

PHP Array Operators

The PHP array operators are used to compare arrays:

Operator Name Example Result

+ Union $x + $y Union of $x and $y (but duplicate keys are not


overwritten)

== Equality $x == $y True if $x and $y have the same key/value pairs

=== Identity $x === $y True if $x and $y have the same key/value pairs in the
same order and of the same types

!= Inequality $x != $y True if $x is not equal to $y

<> Inequality $x <> $y True if $x is not equal to $y

!== Non-identity $x !== $y True if $x is not identical to $y

The example below shows the different results of using the different array operators:

Example
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
$z = $x + $y; // union of $x and $y

var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 9
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

PHP Conditional Statements

Very often when you write code, you want to perform different actions for different
decisions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

if statement - executes some code only if a specified condition is true

if...else statement - executes some code if a condition is true and another code if the
condition is false

if...elseif....else statement - selects one of several blocks of code to be executed

switch statement - selects one of many blocks of code to be executed

PHP - The if Statement

The if statement is used to execute some code only if a specified condition is true.

Syntax

if (condition) {

code to be executed if condition is true;

The example below will output "Have a good day!" if the current time (HOUR) is less than 20:

Example

<?php
$t=date("H");
if ($t<"20")
{
echo "Have a good day!";
}
?>

PHP - The if...else Statement

Use the if....else statement to execute some code if a condition is true and another code if
the condition is false.

Syntax

if (condition)
{
code to be executed if condition is true;
} else
{
code to be executed if condition is false;
}

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 10
Learning Guide 1 – Advance Web Technology
Information Sheet 2 Have a good understanding on Advance Web Tech.

The example below will output "Have a good day!" if the current time is less than 20, and
"Have a good night!" otherwise:

Example

<?php

$t=date("H");

if ($t<"20")

echo "Have a good day!";

} else

echo "Have a good night!";

?>

The example below will output "Have a good morning!" if the current time is less than 10, and
"Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good
night!":

Example

<?php

$t=date("H");

if ($t<"10") {

echo "Have a good morning!";

} elseif ($t<"20") {

echo "Have a good day!";

} else {

echo "Have a good night!";

?>

Course Title: Author: Irwin C. Cansejo, MIT


Advanced Web Technology October 2016 Page 11

You might also like