23
Intro to PHP An introduction to web application development and the PHP programming language

Introduction To Php For Wit2009

  • Upload
    cwarren

  • View
    1.413

  • Download
    2

Embed Size (px)

DESCRIPTION

This is a quick introduction to the basics of PHP.

Citation preview

Page 1: Introduction To Php For Wit2009

Intro to PHPAn introduction to web application

development and the PHP programming language

Page 2: Introduction To Php For Wit2009

What is PHP?

"a widely-used Open Source general-purpose, server-side

scripting language that is especially suited for Web development and can be embedded into HTML"

Page 3: Introduction To Php For Wit2009

dejargonification

• Open Source- free, as in costs no money and modifiable

• general purpose - can do all kinds of different things• server-side - the browser/user gets a normal web page• scripting - connotes loose, easy, and fast– programming rather than layout

• Variables• Conditionals• Looping

• suited to web development - good with text, lots of built-in support

• embedded into HTML - intermixed with normal web page HTML code

Page 4: Introduction To Php For Wit2009

Regular (HTML) Page vs PHP PageHTML Page1. User puts a URL in browser

(‘client’)2. Browser sends request to

host3. Server (on host) receives

request4. Server reads requested file

from hard drive5. Server sends contents of file

to client6. HTML (file contents) goes to

client7. Client (browser) displays

rendered HTML

PHP Page1. User puts URL in browser2. Browser sends request to host3. Server receives request4. Server notes it’s a PHP request,

and passes it to the PHP handler5. PHP handler reads PHP file from

hard drive6. PHP handler executes PHP code

to generate HTML7. PHP handler passes results back

to server8. Server sends results to client9. HTML goes to the client10. Client displays rendered HTML

Page 5: Introduction To Php For Wit2009

Why bother with PHP?• PHP is dynamic – can create HTML based on external,

changing information– System info (e.g. date and time)– Request info (e.g. IP address of client)– Form info (e.g. from info submitted by client)– Info on server (e.g. files, or a DB)

• PHP is modular– Re-use sections of code (functions)– Include files (e.g. navigation headers, libraries)

• PHP can do processing in addition to returning HTML– Send info in an email– Store info in a DB

Page 6: Introduction To Php For Wit2009

General Info

• PHP is typically written in a text editor (e.g. notepad, textpad) or a development environment (e.g. Dreamweaver). You write source code, which is executed/run when the file is requested over the web.

• PHP is intermixed with HTML. You need to know HTML, and all that you do know about HTML will be useful when figuring out what your PHP code should do.

Page 7: Introduction To Php For Wit2009

What and Where

• PHP is in blocks mixed in with regular HTML• A PHP block is marked by <?php to begin it,

and ?> to end it<html> <head><title>A Page</title></head> <body><h1>A Page Title, BIG</h1><?php echo 'text made by PHP code';?> </body></html>

Page 8: Introduction To Php For Wit2009

Basic Statements

NOTE: Each statement ends with a ;Some basic commands:• echo and print – creates text (HTML) that is sent

back to the client– echo 'hello';– print '<h2>Yikes</h2>';

• include – inserts the contents of another file or web page. If including a PHP page that code is executed.– include 'menu.php';

Page 9: Introduction To Php For Wit2009

Variables

• Variables are signified by a $ at the start of a word. The word is the variable name. Variable names can have letters and numbers and underscores, and are case-sensitive– $v– $person1– $first_name

• Assign (change) a variable using =– $i = 5;

Page 10: Introduction To Php For Wit2009

Using Variables

• Store a value in a variable, then use it later– $name = 'Chris';– echo $name;

• Use them in a double quoted string– echo "My name is $name";

• use them to do math$length = 5;$width = 8;$area = $length * $width;

Page 11: Introduction To Php For Wit2009

Arrays• Arrays are created with the array() command

– $people = array(‘Ayesha',‘Pinsi',‘Bret');• Stored values are accessed by an index number, which is in

[] after the variable name. NOTE:The index starts at 0!– $ people[0] is Ayesha– $ people[2] is Bret

• Arrays can hold any kind of value (including other arrays– $mixed = array(4,'Fred',2.5, array(‘red’,’green’));

• Use an array variable with an index the same way you'd use any other variable– $results[3] = 5+6;

Page 12: Introduction To Php For Wit2009

Hashes (Associative Arrays)

Where an array uses a number as the index, a hash (a.k.a. associative array) uses a string.$person = array(‘name’ => ‘Jeff’, ‘class’ => ‘2010’, ‘sport’ => ‘cross country’);

Values are references using the relevant string index (called the ‘key’).$person[‘name’] is Jeff – the key is ‘name’ and the

value is ‘Jeff’

Page 13: Introduction To Php For Wit2009

Operators• Math operators

+, -, *, / standard math• String operators

. join two strings together• Comparison operators (a.k.a. comparators)

>, <, <=, >=, ==• Boolean (true/false) operators

&& (AND, ‘and also’) || (OR, ‘either or both’)T && T = T T || T = TT && F = F T || F = TF && T = F F || T = TF && F = F F || F = F

• Order of operation –when in doubt use ()

Page 14: Introduction To Php For Wit2009

Control Structures - Types• Control structures are special commands that determine

which pieces of PHP actually get executed• There are two main types: conditionals (if statements) and

loops• Conditional – code is executed only when a condition is

true.– if (condition is true) { do this }

• else if (condition) {do this instead}• else {do this if no conditions are true}

• Loop – execute the same section of code repeatedly– while (condition is true) {do this}– for (specific number of times) {do this}– foreach (element in array) {do this}

Page 15: Introduction To Php For Wit2009

Control Structures - Conditions

• A condition is anything that is evaluated to true or false

• For basic values: 0, empty string(''), and empty array( () ) are false, everything else is true

• More conditions are created using comparators, boolean operators (&&, ||), and functions (e.g. is_numeric($foo))

• Combine simple conditions to create more complex ones(($foo == 'cow') || ($days < 4))

Page 16: Introduction To Php For Wit2009

The if statement

Use when you want an action to depend on something:if ($value > 10) {

echo ‘good buy’;} elseif ($value > 5) {

echo ‘consider it’; } else {

echo ‘not worth it’;}

Page 17: Introduction To Php For Wit2009

Loops – while, for, and foreachLoops are used to repeat actions (e.g. creating a table row by row)• while loop – repeat until a certain condition is met

$numLines = 0;while (thereAreMoreLinesToProcess()) {

echo “a line!<br />\n”;$numLines++;

}• for loop – repeat a certain number of times

for ($i = 0; $i < 10; $i++) {echo “line $i<br />\n”;

}• The foreach loop iterates over an array or hash

– foreach ($ar as $elt) { … }– foreach ($ha as $key => $val) { … }

Page 18: Introduction To Php For Wit2009

Functions• A section of code / an action that can be used repeatedly – a

function is kind of a mini-program• A function has a name• A function is used (called) by using its name

– Each time the function is called it does its action– After the function is done it returns to the place from which it was

called• The effects of a function may be controlled through parameters

– The parameters are the things in parentheses that come after the function name – e.g. in displayUser(‘Azd’) the name of the function is displayUser and the parameter is the string ‘Azd’

• Functions may return values– E.g. $area = squareIt(5);

Page 19: Introduction To Php For Wit2009

Built-in Functions

• Built-in functions are pre-made pieces of code that do specific things with or to whatever you give them

• In many ways, the built in function are the language

• PHP has a large set of them

• http://us3.php.net/manual/en/

Page 20: Introduction To Php For Wit2009

Creating Your Own FunctionsThere are four main things to deal with when creating a

function: the name, the parameters, the code, and the returned value.

Functions are created using the special ‘function’ key word

function someName($param1, $param2){

code;code;return value;

}

Page 21: Introduction To Php For Wit2009

Example Functionsfunction sayHi(){

echo “Hello!”;}

function isAllowed($userName,$area){

$retVal = false;if (($area == ‘public’) || ($userName == ‘admin’)){

$retVal = true;}return $retVal;

// PERHAPS BETTER: return (($area == ‘public’) || ($userName == ‘admin’));}

Page 22: Introduction To Php For Wit2009

The include command

The ‘include’ command is very important. It lets you add a existing file into the current page. There are two main ways people use this: –they create segments (header, footer, nav menu, etc.) and then include them on each page – this makes maintenance easier. –they create collections of code and functions (generally called ‘libraries’), so they don’t have to re-define a useful function or repeat code in every page in which they want to use it.

Page 23: Introduction To Php For Wit2009

On Your Own

PHP Manual:http://us3.php.net/manual/en/

XAMPP:http://www.apachefriends.org/en/xampp.html