08 - Introduction To PHP
08 - Introduction To PHP
INTERNET PROGRAMMING
AND E-APPLICATIONS
Lecture 08: Introduction to PHP
PHP: Hypertext Preprocessor
• PHP is a recursive acronym for "PHP:
Hypertext Preprocessor".
• It is a server side scripting language developed
by Rasmus Lerdorf from the Apache Group.
• It is used to manage dynamic content,
databases, session tracking, even build entire
e-commerce sites.
PHP: Hypertext Preprocessor
• PHP scripts are often embedded within HTML
documents.
• The server processes the HTML document,
executing the PHP segments and substituting
the output within the HTML document
• The modified document is then sent to the
client
• The client never sees the PHP code
PHP: Hypertext Preprocessor
• PHP is integrated with a number of popular
databases, including MySQL, PostgreSQL,
Oracle, Sybase, Informix, and Microsoft SQL
Server.
• PHP is forgiving: PHP language tries to be as
forgiving as possible.
Some uses of PHP
• PHP performs system functions, i.e. from files on
a system it can create, open, read, write, and
close them.
• PHP can handle forms, i.e. gather data from files,
save data to a file, through email you can send
data, return data to the user.
• You add, delete, modify elements within your
database through PHP.
• Access cookies variables and set cookies.
• Using PHP, you can restrict users to access some
pages of your website.
• It can encrypt data.
Simple Example
<html>
<head>
<title>Hello Tanzania</title>
</head>
<body>
<?php echo "Hello, Tanzania!";?> i s
eWe b pa g e
</body> re se nt i n th
e PHP p
All of th ge; the
ro m t he p a
d st ri p ped f
o c e ss e d a n n t f ro m the
pr e d to th e c li e
</html> l y th i n g re t urn u t.
on reH T M L o u tp
e rv e r i s p u
Web s
PHP Syntax Overview: PHP Tags
• PHP code is written within the <?php...?> tag
i.e.
<?php PHP code goes here ?>
OR:
<?php
PHP code goes here
?>
PHP Syntax Overview: Semicolons
• A statement in PHP is any expression that is
followed by a semicolon (;).
• Any sequence of valid PHP statements that is
enclosed by the PHP tags is a valid PHP
program.
• Here is a typical statement in PHP, which in this
case assigns a string of characters to a variable
called $greeting :
$greeting = "Welcome to PHP!";
PHP Syntax Overview: Comments
• Single-line comments:
<?php
# This is a comment, and
# This is the second line of the comment
<?php
$capital = 67;
print("Variable capital is $capital<br>");
print("Variable CaPiTaL is $CaPiTaL<br>");
?>
</body>
</html>
PHP Syntax Overview: Expressions are
combinations of tokens
if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.<br>");
}
PHP Data Types
PHP has a total of eight data types which we use to
construct our variables −
1) Integers − are whole numbers, without a decimal point, like
4195.
2) Doubles − are floating-point numbers, like 3.14159 or 49.1.
3) Booleans − have only two possible values either true or false.
4) NULL − is a special type that only has one value: NULL.
5) Strings − are sequences of characters, like 'PHP supports string
operations.'
6) Arrays − are named and indexed collections of other values.
7) Objects − are instances of programmer-defined classes, which
can package up both other kinds of values and functions that are
specific to the class.
8) Resources − are special variables that hold references to
resources external to PHP (such as database connections).
PHP Variables
• 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 Variables
• Variables in PHP do not have intrinsic types - a
variable does not know in advance whether it
will be used to store a number or a string of
characters.
• Variables used before they are assigned have
default values.
• PHP does a good job of automatically
converting types from one to another when
necessary.
PHP Variables: Variable Naming
Rules for naming a variable :
•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.
PHP Variables: Variable Scope
• 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
– Function parameters
– Global variables
– Static variables
Variable Scope: Local variables
• A variable declared in a function is considered
local; that is, it can be referenced solely in
that function. Any assignment outside of that
function will be considered to be an entirely
different variable from the one contained in
the function.
Variable Scope: Local variables
Example:
<?php
$x = 4; • N
ote t
in esc he use of
• A ap ab
nd w ing a $ ch ackslash
function assignx () { mark hat if we aracter. ( \ ) chara
s? us e d cter
single
$x = 0; quot
ation
assignx();
print "\$x outside of function is $x. <br />";
?>
Variable Scope: Function Parameters
• A function is a small unit of program which can
take some input in the form of parameters and
does some processing and may return some
value.
• Function parameters are declared after the
function name and inside parentheses. They are
declared much like a typical variable would be.
Variable Scope: Function Parameters
Example:
<?php
// multiply a value by 10 and return it to the caller
function multiply ($value) {
$value = $value * 10;
return $value;
}
function addit() {
GLOBAL $somevar;
$somevar++;
addit();
?>
Variable Scope: Global Variables
• Example II:
<?php
$somevar = 15;
function addit() {
$somevar=10;
$somevar++;
addit();
print"<br/>";
print "Somevar is $somevar";
?>
Variable Scope: Static Variables
• In contrast to the variables declared as function
parameters, which are destroyed on the
function's exit, a static variable will not lose its
value when the function exits and will still hold
that value should the function be called again.
• You can declare a variable to be static simply by
placing the keyword STATIC in front of the
variable name.
Variable Scope: Static Variables
Example:
<?php <?php
function keep_track() { function keep_track() {
STATIC $count = 0; $count = 0;
$count++; $count++;
print $count; print $count;
print "<br />"; VS print "<br />";
} }
keep_track(); keep_track();
keep_track(); keep_track();
keep_track(); keep_track();
?> ?>
PHP Constants
• A constant is a name or an identifier for a simple value.
• A constant value cannot change during the execution of
the script.
• By default, a constant is case-sensitive.
• By convention, constant identifiers are always uppercase.
• A constant name starts with a letter or underscore,
followed by any number of letters, numbers, or
underscores.
• If you have defined a constant, it can never be changed or
undefined.
PHP Constants
• To define a constant you have to use define()
function and to retrieve the value of a
constant, you simply have to specify its name.
• Unlike with variables, you do not need to have
a constant with a $.
• You can also use the function constant() to
read a constant's value if you wish to obtain
the constant's name dynamically.
PHP Constants: constant() function
• This function returns the value of a constant.
• The function is useful if you need to retrieve the
value of a constant, but do not know its name.
i.e. it is stored in a variable or returned by a
function.
PHP Constants: constant() function
Example:
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the
previous line
?>
End