Unit 1 - PHP
Unit 1 - PHP
PHP started out as a small open source project that evolved as more and more people
found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way
back
in 1994.
PHP supports a large number of major protocols such as POP3, IMAP, and LDAP.
PHP4 added support for Java and distributed object architectures (COM and
CORBA), making n-tier development a possibility for the first time.
PHP performs system functions, i.e. from files on a system it can create, open, read,
write,
and close them. The other uses of PHP are:
PHP can handle forms, i.e. gather data from files, save data to a file, thru email
you can send data, return data to the user.
You add, delete, modify elements within your database thru PHP.
Page | 1
Characteristics of PHP
Five important characteristics make PHP's practical nature possible:
Simplicity
Efficiency
Security
Flexibility
Familiarity
What is PHP?
PHP (PHP: Hypertext Preprocessor) is a widely-used open source general-
purpose scripting language that is especially suited for web development and can
be embedded into HTML.
<?php
// PHP code goes here
?>
<html>
<body>
<?php
echo "Hello World!";
?>
Page | 2
</body>
</html>
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):
Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Output:
Hello World!
Hello World!
Hello World!
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example
<?php
$txt = "Hello world!";
Page | 3
$x = 5;
$y = 10.5;
?>
Run example »
After the execution of the statements above, the variable $txt will hold the value Hello
world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.
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):
Example
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
Output:
My car is red
My house is
My boat is
Page | 4
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.
<html>
<body>
<?php
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
Output
10
Page | 5
Statements are expressions terminated by semicolons
The main way to store information in the middle of a PHP program is by using a
variable.
Here are the most important things to know about variables in PHP.
All variables in PHP are denoted with a leading dollar sign ($).
The value of a variable is the value of its most recent assignment.
Variables are assigned with the = operator, with the variable on the left-hand side
and the expression to be evaluated on the right.
Variables can, but do not need, to be declared before assignment.
PHP has a total of eight data types which we use to construct our variables:
Integers: are whole numbers, without a decimal point, like 4195.
Doubles: are floating-point numbers, like 3.14159 or 49.1.
Page | 6
Booleans: have only two possible values either true or false.
NULL: is a special type that only has one value: NULL.
Strings: are sequences of characters, like 'PHP supports string operations.'
Arrays: are named and indexed collections of other values.
Objects: are instances of programmer-defined classes, which can package up both
other kinds of values and functions that are specific to the class.
Resources: are special variables that hold references to resources external to PHP
(such as database connections).
Integers
They are whole numbers, without a decimal point, like 4195. They are the simplest
type .they correspond to simple whole numbers, both positive and negative. Integers
can
be assigned to variables, or they can be used in expressions, like so:
$int_var = 12345;
$another_int = -12345 + 12345;
Doubles
They like 3.14159 or 49.1. By default, doubles print with the minimum number of
decimal
places needed. For example, the code:
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $many + $many_2;
Boolean
They have only two possible values either true or false. PHP provides a couple of
constants
especially for use as Booleans: TRUE and FALSE, which can be used like so:
if (TRUE)
print("This will always print<br>");
Page | 7
else
print("This will never print<br>");
NULL
NULL is a special type that only has one value: NULL. To give a variable the NULL
value,
simply assign it like this:
$my_var = NULL;
Strings
They are sequences of characters, like "PHP supports string operations". Following are
valid examples of string:
$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, singly quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters
Variable Naming
Rules for naming a variable is:
Variable names must begin with a letter or underscore character.
A variable name can consist of numbers, letters, underscores but you cannot use
characters like + , - , % , ( , ) . & , etc
There is no size limit for variables.
PHP – Variables
Scope can be defined as the range of availability a variable has to the program in which
it
is declared. PHP variables can be one of four scope types:
Local variables
Page | 8
Function parameters
Global variables
Static variables
Page | 9
PHP ─ Operator Types
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic Operators
The following arithmetic operators are supported by PHP language:
Assume variable A holds 10 and variable B holds 20 then:
Example
Try the following example to understand all the arithmetic operators. Copy and paste
following PHP program in test.php file and keep it in your PHP Server's document root
and
browse it using any browser.
<html>
<head><title>Arithmetical Operators</title><head>
Page | 10
<body>
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addition Operation Result: $c <br/>";
$c = $a - $b;
echo "Subtraction Operation Result: $c <br/>";
$c = $a * $b;
echo "Multiplication Operation Result: $c <br/>";
$c = $a / $b;
echo "Division Operation Result: $c <br/>";
$c = $a % $b;
echo "Modulus Operation Result: $c <br/>";
$c = $a++;
echo "Increment Operation Result: $c <br/>";
$c = $a--;
echo "Decrement Operation Result: $c <br/>";
?>
</body>
</html>
Page | 11
Comparison Operators
There are following comparison operators supported by PHP language.
Assume variable A holds 10 and variable B holds 20 then:
Logical Operators
The following logical operators are supported by PHP language.
Assume variable A holds 10 and variable B holds 20 then:
Page | 12
Assignment Operators
PHP supports the following assignment operators:
Conditional Operator
There is one more operator called the conditional operator. It first evaluates an
expression
for a true or false value and then executes one of the two given statements depending
upon the result of the evaluation.
Page | 13
Precedence of PHP Operators
Operator precedence determines the grouping of terms in an expression. This affects
how an expression is evaluated. Certain operators have higher precedence than others;
for example, the multiplication operator has higher precedence than the addition
operator: For example, x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator *
has higher precedence than + so it first get multiplied with 3*2 and then adds into 7.
Here operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom. Within an expression, higher precedence operators will
be evaluated first.
Page | 14
PHP ─ Decision Making
The if, elseif ...else and switch statements are used to take decision based on the
different condition. You can use conditional statements in your code to make your
decisions. PHP supports the following three decision making statements:
if...else statement - use this statement if you want to execute a set of code when
a condition is true and another if the condition is not true
elseif statement - is used with the if...else statement to execute a set of code if
one of several condition are true
switch statement - is used if you want to select one of many blocks of code to be
executed, use the Switch statement. The switch statement is used to avoid long blocks
of if..elseif..else code.
Page | 15
The ElseIf Statement
If you want to execute some code if one of the several conditions is true, then use the
elseif statement.
Page | 16
The Switch Statement
If you want to select one of many blocks of code to be executed, use the Switch
statement. The switch statement is used to avoid long blocks of if..elseif..else code.
Example
Page | 17
OUTPUT
Page | 18
PHP ─ Loop Types
Loops in PHP are used to execute the same block of code a specified number of times.
PHP supports following four loop types.
while - loops through a block of code if and as long as a specified condition is true.
do...while - loops through a block of code once, and then repeats the loop as long
as a special condition is true.
Page | 19
The while loop statement
The while statement will execute a block of code if and as long as a test expression is
true.
If the test expression is true, then the code block will be executed. After the code has
executed the test expression will again be evaluated and the loop will continue until the
test expression is found to be false.
Page | 20
The do...while loop statement
The do...while statement will execute a block of code at least once - it will then repeat
the
loop as long as a condition is true.
Page | 21
PHP ─ Strings
They are sequences of characters, like "PHP supports string operations".
There are no artificial limits on string length - within the bounds of available memory,
you ought to be able to make arbitrarily long strings.
Strings that are delimited by double quotes (as in "this") are preprocessed in both the
following two ways by PHP:
Certain character sequences beginning with backslash (\) are replaced with special
characters
Variable names (starting with $) are replaced with string representations of their
values. The escape-sequence replacements are:
Page | 22
Page | 23
PHP ─ Arrays
An array is a data structure that stores one or more similar type of values in a single
value.
For example, if you want to store 100 numbers, then instead of defining 100 variables, it
is easy to define an array of 100 length.
There are three different kind of arrays and each array value is accessed using an ID c
which is called array index.
Numeric array - An array with a numeric index. Values are stored and accessed
in linear fashion
Multidimensional array - An array containing one or more arrays and values are
accessed using multiple indices
Numeric Array
These arrays can store numbers, strings and any object but their index will be
represented
by numbers. By default, the array index starts from zero.
Page | 24
Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an array. And
each
element in the sub-array can be an array, and so on. Values in the multi-dimensional
array
are accessed using multiple index.
Example
Page | 25
Page | 26