PHP Basics-Web Programming-Lecture Slides, Slides of Web Programming and Technologies

This lecture was delivered by Prof. Arun Ullal at Ankit Institute of Technology and Science for Web Programming course. It includes: PHP, Basics, Syntax, Functions, Variables, Operators, Forms, Cookies, User-entered, Data

Typology: Slides

2011/2012

Uploaded on 07/18/2012

palvani
palvani 🇮🇳

4.5

(2)

83 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP519: Web Programming
Autumn 2011
PHP Basics:
Introduction to PHP
a PHP file, PHP workings, running PHP.
Basic PHP syntax
Basic PHP syntax
variables, operators, if...else...and switch, while, do while, and for.
Some useful PHP functions
How to work with
HTML forms, cookies, files, time and date.
How to create a basic checker for user-entered data
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download PHP Basics-Web Programming-Lecture Slides and more Slides Web Programming and Technologies in PDF only on Docsity!

COMP519: Web Programming

Autumn 2011

PHP Basics: 

Introduction to PHP

• a PHP file, PHP workings, running PHP.

Basic PHP syntax

Basic PHP syntax

• variables, operators, if...else...and switch, while, do while, and for.

Some useful PHP functions

How to work with

• HTML forms, cookies, files, time and date.

How to create a basic checker for user-entered data

Server-Side Dynamic Web Programming

•^

CGI is one of the most common approaches to server-side programming^ 

Universal support:

(almost) Every server supports CGI programming. A great deal of ready-to-use

CGI code. Most APIs (Application Programming Interfaces) also allow CGI programming.

^

Choice of languages:

CGI is extremely general, so that programs may be written in nearly any

language. Perl is by far the most popular, with the result that many people think that CGI meansPerl. But C, C++, Ruby, and Python are also used for CGI programming.

^

Drawbacks:

A separate process is run every time the script is requested. A distinction is made

between HTML pages and code.

•^

Other server

- side alternatives try to avoid the drawbacks

•^

Other server

- side alternatives try to avoid the drawbacks

^

Server-Side Includes (SSI):

Code is

embedded

in HTML pages, and evaluated on the server while

the pages are being served. Add dynamically generated content to an existing HTML page, withouthaving to serve the entire page via a CGI program.

^

Active Server Pages (ASP, Microsoft)

: The ASP engine is integrated into the web server so it does

not require an additional process. It allows programmers to mix code within HTML pages instead ofwriting separate programs. (

Drawback

(?) Must be run on a server using Microsoft server software.)

^

Java Servlets (Sun):

As CGI scripts, they are code that creates documents. These must be

compiled as classes which are dynamically loaded by the web server when they are run.

^

Java Server Pages (JSP):

Like ASP, another technology that allows developers to embed Java in

web pages.

What do You Need?

•^

Our server supports PHP

You don't need to do anything special! *

You don't need to compile anything or install any extra tools!

Create some .php files in your web directory - and the server will parse them for you.

  • Slightly different rules apply when dealing with an SQL database (as will be explained when we get to that point).

•^

Most servers support PHP

Download PHP for free here:

http://www.php.net/downloads.php

Download PHP for free here:

http://www.php.net/downloads.php

Download MySQL for free here:

http://www.mysql.com/downloads/index.html

Download Apache for free here:

http://httpd.apache.org/download.cgi

(Note: All of this is already present on the CS servers, so you need not do any installation

yourself to utilize PHP on our machines.)

•Loads of information, including help on individual PHP functionsmay be found at

http://uk.php.net/

Help with PHP

Scalars

All variables in PHP start with a

$

sign symbol. A variable's type is determined by the

context in which that variable is used (i.e. there is no strong-typing in PHP

).

\n";

$txt='1234';

echo "$txt

\n";

$a

=

1234;

echo

"$a

\n";

$a

=

-123;

echo

"$a

\n";

$a

=

1.234;

echo

"$a

\

n";

Four scalar types: boolean

true or false integer, float,

echo

"$a

\

n";

$a

=

1.2e3;

echo

"$a

\n";

$a

=

7E-10;

echo

"$a

\n";

echo

'Arnold

once said:

"I'll

be back"',

"

\n";

$beer

=

'Heineken';

echo

"$beer's

taste

is

great

\n";

$str

=

<<

float,^ floating point numbers string^ single quoteddouble quoted

view the output page

Arrays

An array in PHP is actually an ordered map. A map is a type that maps values to keys.

array()

= creates arrays

"bar", 12 => true);echo $arr["foo"]; // barecho $arr[12];

key

= either an integer or a string.

value

= any PHP type.

43, 32, 56, "b" => 12); array(5 => 43, 6 => 32, 7 => 56, "b" => 12);

if^

no key given

(as in example), the

PHP interpreter uses (maximum of the integer indices + 1).

array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?>

the integer indices + 1). if^

an existing key

, its value will be

overwritten.

1, 12 => 2); foreach

($arr as $key => $value) { echo $key, ‘=>’,

$value);

$arr[] = 56;

// the same as $arr[13] = 56;

$arr["x"] = 42; // adds a new elementunset($arr[5]); // removes the elementunset($arr);

// deletes the whole array

$a = array(1 => 'one', 2 => 'two', 3 => 'three');unset($a[2]);$b = array_values($a);?>

can set values in an array

unset()

removes a

key/value pair

*Find more on arrays

array_values() makes reindexing effect(indexing numerically)

view the output page

Operators

•^

Arithmetic Operators:

•^

Assignment Operators:

•^

Comparison Operators:

•^

Logical Operators:

Example

Is the same as

x+=y

x=x+y

x-=y

x=x-y

x*=y

x=x*y

x/=y

x=x/y

x%=y

x=x%y

•^

Logical Operators:

•^

String Operators

:^

.^

and

(for string concatenation)

$a = "Hello ";$b = $a. "World!"; // now $b contains "Hello World!"$a = "Hello ";$a .= "World!";

Conditionals: if else

Can execute a set of code depending on a condition

”; if

$d=="Fri"

echo "Have a nice weekend! ";

else

if (

condition

code to be executed if conditionis

true

else code to be executed if conditionis

false

else

echo "Have a nice day! ";

$x=10;if ($x==10){

echo "Hello";echo "Good morning";

} ?>

date() view the output page

is a built-in PHP function

that can be called with manydifferent parameters to return thedate (and/or local time) invarious formatsIn this case we get a three letterstring for the day of the week.

Looping: while and do-while

Can loop depending on a condition

";$i++; }

"; }

} ?> loops through a block of code if, andas long as, a specified condition istrue

view the output page

} while

$i <= 10

?>

loops through a block of code once,and then repeats the loop as longas a special condition is true (sowill always execute at least once)

view the output page

Looping: for and foreach

Can loop depending on a "counter"

"; } ?> loops through a block of code a

\n”; } ?>

loops through a block of code a specified number of times

loops through a block of code for eachelement in an array

$value)

echo $key." = ".$value."\n"; } ?>

view the output page

Variable Scope

The scope of a variable is the context within which it is defined.

The scope is local within functions,and hence the value of $a isundefined in the “echo” statement.

global refers to itsglobalversion.

static does not loseits value.

view the output page

Including Files

The

include()

statement includes and evaluates the specified file.

vars.php test.php

*The scope of variables in “included” files depends on where the “include” file is added! You can use the include_once, require, and require_once statements in similar ways

view the output page

/*

vars.php is

in

the

scope

of

foo()

so

*^

$fruit

is

NOT

available

outside

of

this

*^

scope.

$color

is

because

we

declared

it

*^

as

global.

*/

foo();

//

A

green

apple

echo "A

$color

$fruit";

//

A

green

?>

view the output page

Server Variables

The

$_SERVER

array variable is a reserved variable that contains all server information.

";

echo "Browser: ".

$_SERVER["HTTP_USER_AGENT"]

. "";

echo "User's IP address: ".

$_SERVER["REMOTE_ADDR"]

?> ";echo "All information";foreach ($_SERVER as $key => $value)

echo $key. " = ". $value. ""; }

?> The $_SERVER is a super global variable, i.e. it's available in all scopes of a PHP script.

view the output page

$_SERVER infoon php.net

File Open

The

fopen(

"file_name"

,

"mode"

)

function is used to open files in PHP.

(use with caution

, i.e. check that this is the case,

otherwise you’ll overwrite an existing file).

For

x

if a file exists, this function fails (and

returns 0).