+ All Categories
Home > Documents > Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

Date post: 31-Mar-2015
Category:
Upload: kasandra-taney
View: 213 times
Download: 0 times
Share this document with a friend
25
5-May-2007 : [1] BBK P1 Module Session 1 & 2 PHP: The Basics
Transcript
Page 1: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [1]BBK P1 ModuleSession 1 & 2

PHP: The Basics

Page 2: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [2]BBK P1 ModuleSession 1 & 2

What is it?

• PHP is a scripting language commonly used on web servers.– Stands for “PHP: Hypertext Preprocessor”– Open source– Embedded code– Comparable with ASP– Multiple operating systems/web servers

Page 3: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [3]BBK P1 ModuleSession 1 & 2

The PHP Resource

www.php.net

Page 4: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [4]BBK P1 ModuleSession 1 & 2

What can it do?• Dynamic generation of web-page content• Database interaction• Processing of user supplied data• Email• File handling• Text processing• Network interaction• And more…

Page 5: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [5]BBK P1 ModuleSession 1 & 2

Fundamentals

• PHP is embedded within xhtml pages within the tags: <?php … ?>

• The short version of these tags can also be used: <? … ?>

• Each line of PHP is terminated, like MySQL, with a semi-colon.

Page 6: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [6]BBK P1 ModuleSession 1 & 2

Hello World!

<html> <head> <title>PHP Test</title> </head> <body> <?php echo ‘<p>Hello World!</p>’; ?> </body> </html>

Page 7: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [7]BBK P1 ModuleSession 1 & 2

Let’s get started then..

• PHP is enabled on the Birkbeck server• Create your pages in your personal space• Access them via

– http://students.dcs.bbk.ac.uk/userid/web/– This points to your I:\web\ directory

• REMEMBER– .php extension so server knows to parse it!– Use full web address not local path

• Otherwise server will not parse the file

Page 8: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [8]BBK P1 ModuleSession 1 & 2

HOE 1 : Preparing to code with PHP

Page 9: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [9]BBK P1 ModuleSession 1 & 2

Literals..

• All strings must be enclosed in single of double quotes: ‘Hello’ or “Hello”.

• Numbers are not in enclosed in quotes: 1 or 45 or 34.564

• Booleans (true/false) can be written directly as true or false.

Page 10: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [10]BBK P1 ModuleSession 1 & 2

Comments

// This is a comment

# This is also a comment

/* This is a commentthat is spread overmultiple lines */

• Do not nest multi-line comments

• // recommended over #

Page 11: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [11]BBK P1 ModuleSession 1 & 2

Comments

<?php

// this is a comment

echo ‘Hello World!’;

/* another

multi-line comment */

?>

Page 12: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [12]BBK P1 ModuleSession 1 & 2

Displaying Data

• There are two language constructs available to display data: print() and echo().

• They can be used with or without brackets.

• Note that the data ‘displayed’ by PHP is actually parsed by your browser as HTML. View source to see actual output.

Page 13: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [13]BBK P1 ModuleSession 1 & 2

Displaying data

<?php

echo ‘Hello World!<br />’;echo(‘Hello World!<br />’);

print ‘Hello World!<br />’;print(‘Hello World!<br />’);

?>

Page 14: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [14]BBK P1 ModuleSession 1 & 2

Escaping Characters

• Some characters are considered ‘special’

• Escape these with a backslash \

• Special characters will be flagged when they arise, for example a double or single quote belong in this group…

Page 15: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [15]BBK P1 ModuleSession 1 & 2

Escaping Characters

<?php// Claire O’Reilly said “Hello”.

echo ‘Claire O\’Reilly ’;

echo “said \”Hello\”.”;

?>

Page 16: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [16]BBK P1 ModuleSession 1 & 2

Variables: What are they?

• When we work in PHP, we often need a labelled place to store a value (be it a string, number, whatever) so we can use it in multiple places in our script.

• These labelled ‘places’ are called

VARIABLES

Page 17: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [17]BBK P1 ModuleSession 1 & 2

Variables: Naming

• $ followed by variable name

• Case sensitive– $variable differs from $Variable– Stick to lower-case to be sure!

• Name must started with a letter or an underscore– Followed by any number of letters, numbers

and underscores

Page 18: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [18]BBK P1 ModuleSession 1 & 2

Variables: example

<?php$name = ‘Phil’;$age = 23;echo $name;echo ’ is ‘;echo $age;// Phil is 23 ?>

Page 19: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [19]BBK P1 ModuleSession 1 & 2

Constants

• Constants (unchangeable variables) can also be defined.

• Each constant is given a name (note no preceding dollar is applied here).

• By convention, constant names are usually in UPPERCASE.

Page 20: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [20]BBK P1 ModuleSession 1 & 2

Constants

<?phpdefine(‘NAME’,‘Phil’);define(‘AGE’,23);echo NAME;echo ’ is ‘;echo AGE;// Phil is 23 ?>

Page 21: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [21]BBK P1 ModuleSession 1 & 2

“ or ‘ ?

• There is a difference between strings written in single and double quotes.

• In a double-quoted string any variable names are expanded to their values.

• In a single-quoted string, no variable expansion takes place.

Page 22: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [22]BBK P1 ModuleSession 1 & 2

“ or ‘ ?

<?php$name = ‘Phil’;$age = 23;echo “$name is $age”;// Phil is 23 ?>

Page 23: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [23]BBK P1 ModuleSession 1 & 2

“ or ‘ ?

<?php$name = ‘Phil’;$age = 23;echo ‘$name is $age’;// $name is $age ?>

Page 24: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [24]BBK P1 ModuleSession 1 & 2

HOE 2 : The Fundamentals

Page 25: Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.

5-May-2007 : [25]BBK P1 ModuleSession 1 & 2

Review

• We’ve started PHP..– Escaping XHTML– Comments– Basic Syntax– Variables– Constants


Recommended