0% found this document useful (0 votes)
15 views34 pages

08 - Introduction To PHP

Introduction to php

Uploaded by

meshackmlyanginu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
15 views34 pages

08 - Introduction To PHP

Introduction to php

Uploaded by

meshackmlyanginu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 34

RCS 301

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

// This is a comment too.


print "An example with single line comments";
?>
PHP Syntax Overview: Comments
• Multi-line comments:
<?php
/* This is a multiline comment
Author : Mohammad Mohtashim
Purpose: Multiline Comments Demo
Subject: PHP
*/

print "An example with multi-line comments";


?>
PHP Syntax Overview: Whitespaces
• Whitespace is the stuff you type that is
typically invisible on the screen, including
spaces, tabs, and carriage returns (end-of-line
characters).
• PHP whitespace insensitive means that it
almost never matters how many whitespace
characters you have in a row.one whitespace
character is the same as many such
characters.
PHP Syntax Overview: Whitespaces
• For example, each of the following PHP
statements that assigns the sum of 2 + 2 to the
variable $four is equivalent −
$four = 2 + 2; // single spaces
$four <tab>=<tab>2<tab>+<tab>2 ; // spaces and
tabs
$four =
2+
2; // multiple lines
PHP Syntax Overview: Case Sensitivity
• PHP is case sensitive.
• Try out this code:
<html>
<body>

<?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

• The smallest building blocks of PHP are the


indivisible tokens, such as numbers (3.14159),
strings (.two.), variables ($two), constants
(TRUE), and the special words that make up
the syntax of PHP itself like if, else, while, for
and so forth
PHP Syntax Overview: Braces make blocks
• Although statements cannot be combined like
expressions, you can always put a sequence of
statements anywhere a statement can go by enclosing
them in a set of curly braces.
• Here both statements are equivalent:
if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.<br>");

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

print "\$x inside function is $x. <br />";


}

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;
}

$retval = multiply (10);


Print "Return value is $retval\n";
?>
Variable Scope: Global Variables
• In contrast to local variables, a global variable
can be accessed in any part of the program.
• However, in order to be modified, a global
variable must be explicitly declared to be global
in the function in which it is to be modified.
• This is accomplished, conveniently enough, by
placing the keyword GLOBAL in front of the
variable that should be recognized as global.
• Placing this keyword in front of an already
existing variable tells PHP to use the variable
having that name.
Variable Scope: Global Variables
• Example I:
<?php
$somevar = 15;

function addit() {
GLOBAL $somevar;
$somevar++;

print "Somevar is $somevar";


}

addit();
?>
Variable Scope: Global Variables
• Example II:
<?php
$somevar = 15;

function addit() {
$somevar=10;
$somevar++;

print "Somevar is $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

You might also like