0% found this document useful (0 votes)
10 views

Adv Web Tech

Uploaded by

demo work
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Adv Web Tech

Uploaded by

demo work
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

PHP & MySQL

UNIT ONE

LECTURE NOTES BY: UMAR FAROUK


 RECOMMENDED BOOKS
 Learning PHP, MYSQL, and Javascript

◦ By ROBIN NIXON
◦ Publishers- Oreilly

Build your own Database Driven Website Using PHP


and Mysql
By Kelvin Yank
Publishers- Sitepoint.com

LECTURE NOTES BY: UMAR FAROUK


UNIT ONE
INTRODUCTION TO DYNAMIC CONTENT
* HTTP
* WEB SERVERS
* WEB DEVELOPMENT LANGUAGES
* SETTING UP A DEVELOPMENT ENVIRONMENT
UNIT TWO
INTRODUCTION TO PHP
* THE PHP LANGUAGE

LECTURE NOTES BY: UMAR FAROUK


UNIT THREE
EXPRESSION AND CONTROL FLOW IN PHP
* EXPRESSIONS
* OPERATORS
* CONDITIONALS
* LOOPING
* IMPLICIT AND EXPLICIT CASTING

UNIT FOUR
PHP FUNCTIONS AND OBJECTS
* FUNCTIONS
* INCLUDING AND REQUIRING FILES
* PHP OBJECTS (OBJECT ORIENTED PROGRAMMING)

LECTURE NOTES BY: UMAR FAROUK


UNIT FIVE
MYSQL ADMINISTRATION

UNIT SIX
FORM HANDLING
* BUILDING FORMS
UNIT SEVEN
• COOKIES, SESSION AND AUTHENTICATION
UNIT EIGHT
PROJECT

LECTURE NOTES BY: UMAR FAROUK


 HTTP

 WEB SERVERS

 WEB DEV. LANGUAGES

 INTRANET/ INTERNET APPLICATIONS

LECTURE NOTES BY: UMAR FAROUK


 HTTP is a communication standard governing the
requests and responses that take place between the
browser running on the end user’s computer and the
web server.

 The server’s job is to accept a request from the client


and attempt to reply to it in a meaningful way,
usually by serving up a requested web page—that’s
why the term server is used

LECTURE NOTES BY: UMAR FAROUK


LECTURE NOTES BY: UMAR FAROUK
 A web server can usually handle multiple
simultaneous connections and—when not
 communicating with a client—spends its time

listening for an incoming connection.

 When one arrives, the server sends back a


response to confirm its receipt

LECTURE NOTES BY: UMAR FAROUK


1. You enter http://server.com into your
browser’s address bar.

2. Your browser looks up the IP address for


server.com.

3. Your browser issues a request for the home


page at server.com.

4. The request crosses the Internet and arrives at


the server.com web server.

5. The web server, having received the request,


looks for the web page on its hard disk.
LECTURE NOTES BY: UMAR FAROUK
6. The web page is retrieved by the server and
returned to the browser.

7. Your browser displays the web page.

LECTURE NOTES BY: UMAR FAROUK


WEB SERVERS (SOFTWARE)

 Apache
 IIS
 Novell's Web Server (Novel OS)
 Lotus Domino servers (IBM)
 JBoss(Mostly used in Eclipse)
 GlassFish (For Java EEE using netbeans)

LECTURE NOTES BY: UMAR FAROUK


 Other Web servers include :
 Novell's Web Server for users of its NetWare

operating system

 IBM's family of Lotus Domino servers,


primarily for IBM's OS/390 and AS/400
customers.

 Assignment look for other Web Servers

LECTURE NOTES BY: UMAR FAROUK


 WEB PROGRAMMING

LECTURE NOTES BY: UMAR FAROUK


WEB PROGRAMMING LANGUAGES

 PHP
 ASP.NET
 RUGBY RAIL
 JAVA
 PHTHON
 etc

LECTURE NOTES BY: UMAR FAROUK


WEB DEVELOPMENT ENVIRONMENT
 Developing a Web Application in PHP does not require and
special program.

 Therefore what you need is a text editor or a program that


supports text editing.
 Eg. NotePad
 NotePad ++
 DreamWeaver
 Netbeans
 You can also use a Framework
 Eg. CakePHP, EasyPHP, Laravel etc

 A Web Server (Localhost)


LECTURE NOTES BY: UMAR FAROUK
Introduction to PHP

UNIT TWO
 PHP is a recursive acronym for Hypertext
Preprocessor
 Developed by…Rasmus Lerdorf

 Year :1994

 Website: http://www.php.net

LANGUAGE SYNTAX

Basic Syntax

PHP is quite a simple language with roots in C and


Perl, yet looks more like Java.

Semicolons

PHP statement ends with a semi colon


Eg. $x += 10;
 The PHP parser faster, as it instantly knows whenever
it comes across a variable.
COMMENTS

There are two ways

SINGLE LINE COMMENT


// This is a comment
$x += 10; // Increment $x by 10

MULTY LINE COMMENTS

You can use the /* and */ pairs of characters to open and


close comments .
<?php
/* This is a section
of multiline comments
which will not be
interpreted */
?>
Variable - The $ symbol

PHP is a loosely typed language. This means that a single variable


may contain any type of data, be it a number, string of text and
may change types over its lifetime.

$ is used to precede variable names. These variables can be


numbers and string.

String variables
$username = "Kojo Mensah";
The quotation marks indicate that “Fred Smith” is a string of
characters. You must enclose each string in quotation marks
Numeric variables

$count = 17;
You could also use a floating-point number (containing
a decimal point);

The syntax is the same:


$count = 17.5;
CASTING- Implicit and Explicit Casting

It also automatically converts values from one type to


another whenever required. This is called implicit casting.

This expression returns a floating-point number


<?php
$a = 56;
$b = 12;
$c = $a / $b;
echo $c;
?>
To cast $c to be an integer instead. $c = (int) ($a / $b);
This is called explicit casting.

PHP’s cast types


Cast type Description
(int) (integer) Cast to an integer by dropping
the decimal portion

(bool) (boolean) Cast to a Boolean

(float) (double) (real) Cast to a floating-point number

(string) Cast to a string


VARIABLE NAMING RULES
When creating PHP variables, you must follow these four rules:

• Variable names must start with a letter of the alphabet or the _


(underscore) character.

• Variable names can contain only the characters: a-z, A-Z, 0-9, and _
(underscore).

• Variable names may not contain spaces. If a variable must comprise


more than one word it should be separated with the _ (underscore)
character. (e.g., $user_name).

• Variable names are case-sensitive. The variable $High_Score is not the


same as the variable $high_score.
VARIABLE SCOPE
Local variables
Local variables are variables that are created within and can
be accessed only by a function.

They are generally temporary variables that are used to store


partially processed results prior to the function’s return.

<?php
function longdate($timestamp)
{
$temp = date("l F jS Y", $timestamp);
return "The date is $temp";
}
Global variables

There are cases when you need a variable to have global


scope, because you want all your code to be able to access
it.

Also, some data may be large and complex, and you don’t
want to keep passing it as arguments to functions.

To declare a variable as having global scope, use the


keyword global (use global variables with caution)
global $balance;
Static variables

<?php
function test()
{
static $count = 0;
echo $count;
$count++;
}
?>
$var1=‘PHP’; Assign a value of ‘PHP’ to $var1
$var2=5;
$var3=$var2 +1;
$var2= $var1;
echo $var1; // out PHP
echo $var2; //
echo $var3 //
echo $var2. ‘rule’; // Outputs ‘PHP rule’
Superglobal variables

Starting with PHP 4.1.0, several predefined variables


are available.

These are known as super global variables, which


means that they are provided by the PHP environment
but are global within the program, accessible absolutely
everywhere.

These super globals contain lots of useful information


about the currently running program and its
environment
Superglobal name Contents

$GLOBALS All variables that are currently defined in the

global scope of the script. The variable names are the keys of the

array.

$_SERVER Information such as headers, paths, and script

locations. The entries in this array are created by the web server

and there is no guarantee that every web server will provide any

or all of these.

$_POST Variables passed to the current script via the HTTP POST

method.

$_FILES Items uploaded to the current script via the HTTP POST

method.

$_COOKIE Variables passed to the current script via HTTP cookies.

$_SESSION Session variables available to the current script.

$_REQUEST Contents of information passed from the browser; by

default, $_GET, $_POST and $_COOKIE.

$_ENV Variables passed to the current script via the environment

method.
$_GET Variables passed to the current script via the
HTTP GET method

All of the super globals are named with a single initial


underscore and only capital letters; therefore, you
should avoid naming your own variables in this manner
to avoid
OPERATORS

Operators are the mathematical, string, comparison, and


logical commands such as plus, minus, times, and divide.

PHP looks a lot like plain arithmetic; for instance, the


following statement outputs 8:
 echo 6 + 2;

ARITHMETIC OPERATORS:

They are used to perform mathematics computation


Operator Description Example
+ Addition $j + 1
- Subtraction $j - 6
* Multiplication $j * 11
/ Division $j / 4
% Modulus $j % 9
(division remainder)
++ Increment ++$j
-- Decrement --$j
Operator Description

&& And $j == 3 && $k == 2

and $k == 2

|| Or $j < 5 || $j > 10

or $j < 5 or $j > 10

! Not ! ($j == $k)

xor Exclusive or $j xor $k



Note that && is usually interchangeable with and;
the same is true for || and or.

But and and or have a lower precedence, so in some


cases, you may need extra parentheses to force the
required precedence.

On the other hand, there are times when only and or or


are acceptable,

Eg
mysql_select_db($database) or die("Unable to select
database");
Multiple-Line Commands

There are times when you need to output quite a lot of text from
PHP and using several echo (or print) statements would be time-
consuming and messy.

To overcome this, PHP offers two conveniences. The first is just to


put multiple lines between quotes, as

<?php
$lecturer = “Emmanuel ";
echo “This a Advance Web Class
offered by the second year class.
Regular/Evening and Weekend
Taught by $lecturer .";
?>
here-document or heredoc

PHP also offers a multiline sequence using the <<< operator,


commonly referred to as here-document or heredoc for short,

<?php
$author = "Alfred E Newman";
echo <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_END;
?>
What this code does is to tell PHP to output everything
between the two _END tags as if it were a double-quoted
string.

It is important to remember that the closing _END; tag


must appear right at the start of a new line and it must be
the only thing on that line—not even a comment is
allowed.
EXPRESSION AND FLOW OF CONTROL

UNIT THREE
An expression is a combination of values,
variables, operators, and functions that results
in a value.

y = 3(abs(2x) + 4)
which in PHP would be:

$y = 3 * (abs(2*$x) + 4);

Literals and Variables




CONDITIONALS

There are three types of non-looping conditionals: the


if statement, the switch statement, and the ? operator

The if Statement

<?php
if ($bank_balance < 100)
{
$money += 1000;
$bank_balance += $money;
}
?>
The else Statement
<?php
if ($bank_balance < 100)
{
$money += 1000;
$bank_balance += $money;
}
else
{
$savings += 50;
$bank_balance -= 50;
}
?>
The elseif Statement

There are also times when you want a number of different possibilities to
occur, based upon a sequence of conditions.

<?php
if ($bank_balance < 100)
{
$money += 1000;
$bank_balance += $money;
}
elseif ($bank_balance > 200)
{
$savings += 100;
$bank_balance -= 100;
}
else
{
$savings += 50;
$bank_balance -= 50;
}
?>
A multiple-line if...elseif... statement

<?php
if ($page == "Home") echo "You selected
Home";
elseif ($page == "About") echo "You selected
About";
elseif ($page == "News") echo "You selected
News";
elseif ($page == "Login") echo "You selected
Login";
elseif ($page == "Links") echo "You selected
Links";
switch statement
<?php
switch ($page)
{
case "Home": echo "You selected Home";
break;
case "About": echo "You selected About";
break;
case "News": echo "You selected News";
break;
case "Login": echo "You selected Login";
break;
case "Links": echo "You selected Links";
break;
}
?>
Breaking out

If you wish to break out of the switch statement


because a condition has been fulfilled, use the
break command. This command tells PHP to
break out of the switch and jump
to the following statement.

.
The ? Operator
One way of avoiding the verbosity of if and else statements
is to use the more compact ternary operator, ?, which is
unusual in that it takes three operands rather than the
more usual two.

The ? operator is passed an expression that it must


evaluate, along with two statements
to execute: one for when the expression evaluates to TRUE,
the other for when it is FALSE.

Using the ? operator

<?php
echo $fuel <= 1 ? "Fill tank now" : "There's enough fuel";
?>
LOOPING
A while loop
<?php
$fuel = 10;
while ($fuel > 1)
{
// Keep driving ...
echo "There's enough fuel";
}
?>
$count = 0;
while (++$count <= 12)
echo "$count times 12 is " . $count * 12 .
"<br />";
?>

In this example, it possible to remove the ++


$count statement from inside the while loop
and place it directly into the conditional
expression of the loop.
 What now happens is that PHP encounters the
variable $count at the start of each iteration
of the loop and, noticing that it is prefaced
with the increment operator, first increments
the variable and only then compares it to the
value 12.
do...while Loops

<?php
$count = 1;
do
echo "$count times 12 is " . $count * 12 .
"<br />";
while (++$count <= 12);
?>
For Loops

<?php
for ($count = 1 ; $count <= 12 ; ++$count)
echo "$count times 12 is " . $count * 12 .
"<br />";
?>
Each for statement takes three parameters:
An initialization expression
A condition expression
A modification expression
The for loop from with added curly braces
<?php
for ($count = 1 ; $count <= 12 ; ++$count)
{
echo "$count times 12 is " . $count * 12;
echo "<br />";
}
?>

You might also like