PHP Introduction ATLS 3020 Digital Media 2 Aileen Pierce Client - - PowerPoint PPT Presentation

php introduction
SMART_READER_LITE
LIVE PREVIEW

PHP Introduction ATLS 3020 Digital Media 2 Aileen Pierce Client - - PowerPoint PPT Presentation

PHP Introduction ATLS 3020 Digital Media 2 Aileen Pierce Client vs. Server Side Scripting Client-side scripting (browser) Designed to interact with the user s web page Event-driven Restricted to what a web browser can


slide-1
SLIDE 1

PHP Introduction

ATLS 3020 Digital Media 2 Aileen Pierce

slide-2
SLIDE 2

Client vs. Server Side Scripting

¤ Client-side scripting (browser)

¤ Designed to interact with the user’s web page ¤ Event-driven ¤ Restricted to what a web browser can do ¤ Limited by not knowing the client system

¤ Server-side scripting (server)

¤ Integrates dynamic content into web pages ¤ Database integration ¤ The user doesn't see your source code ¤ Controlled computer environment

slide-3
SLIDE 3

What is PHP?

¤ PHP stands for Hypertext Pre-Processor ¤ Embedded server-side scripting language ¤ Cross platform ¤ Open source software ¤ Specifically developed for dynamic web applications ¤ Developed in 1994 by Rasmus Lerdorf

slide-4
SLIDE 4

Why PHP?

¤ Allows developers to write dynamically generated web pages quickly ¤ How about other scripting languages like Perl, Tcl, and Python?

¤ Created specifically for dynamic Web-page creation ¤ Faster to execute than CGI scripts ¤ Easier and faster to learn

¤ Easily interacts with databases and files

slide-5
SLIDE 5

How does PHP Work?

¤ A web browser requests a page ¤ The request is sent to the web server ¤ If the page contains PHP it is sent to the PHP parsing engine ¤ The PHP parsing engine executes the PHP code and creates the HTML source of the web page ¤ The Web server sends the HTML page back to the web browser ¤ The browser renders the web page

slide-6
SLIDE 6

PHP Overview

¤ Need a web server with PHP installed ¤ .php file extension ¤ Can have a mix of HTML and PHP ¤ PHP tags

<?php ?>

¤ Generates a valid web page ¤ Semicolon (;) at the end of each instruction

slide-7
SLIDE 7

PHP Variables

¤ Variables in PHP, like JavaScript, can hold strings, numbers, or objects

¤ Variable names must start with a dollar sign ($) ¤ $peaches = 1; ¤ “var” is not needed

¤ No spaces or punctuation except for _ ¤ Case sensitive ¤ The first character after the $ cannot be a number ¤ Use descriptive names, but not too long

slide-8
SLIDE 8

Output

¤ echo and print both create output ¤ echo "message"; or print "message";

¤ Outputs “message” as text to the page

¤ echo $username; or print $username;

¤ Outputs the value of $username to the page

slide-9
SLIDE 9

Output

¤ Variables can be printed in double quotes

¤ $x = 10; ¤ print "Mom, please send $x dollars"; ¤ Mom, please send 10 dollars

¤ You can’t print variables in single quotes

¤ print 'Mom, please send $x dollars’; ¤ Mom, please send $x dollars

¤ To output a single variable’s value or expression,

  • mit the quotation marks.

¤ print $x*2; ¤ 20

slide-10
SLIDE 10

Quotes

¤ Single or double quotes are OK ¤ Numbers don’t need quotes, text does ¤ Use double quotes if the text has any apostrophes or escape out apostrophes

¤ $thing = “This is David’s book.”; ¤ $thing = ‘This is David\’s book.’;

¤ Boolean values of true, false, and null should not be enclosed in quotes.

slide-11
SLIDE 11

PHP Strings

¤ Strings are joined with a dot (.) instead of a +

echo ‘Aileen ’ . ‘J’; Aileen J $name = ‘Aileen ’ . ‘J ’; echo $name; Aileen J $name .= ‘Pierce’; echo $name; Aileen J Pierce

slide-12
SLIDE 12

Generating HTML Tags

¤ You can insert any HTML tags you want to in the PHP output.

¤ print "<h3>Hi there, $name</h3>"; ¤ print '<p>'; ¤ print "</p>"; ¤ print '<br>';

slide-13
SLIDE 13

PHP Comments

¤ PHP comments aren’t visible to the end user ¤ PHP line comments

# This is a comment // This is another comment

¤ PHP multiple line comments

/* This is a longer comment That spans two lines */

slide-14
SLIDE 14

Conditionals and Loop Statements

¤ The following PHP statements use the same syntax as JavaScript

¤ if/else ¤ while loop ¤ for loop

¤ The only difference is variables in PHP have a $ in front of the variable name.

slide-15
SLIDE 15

PHP’s Simple if/else Statement

¤ PHP has a shorthand if/else statement if(condition) { true statement } else { false statement } ¤ Works well when there is just one true statement and one false (else) statement

¤ Statements can be function calls

slide-16
SLIDE 16

Lab

¤ Create a simple PHP script that uses a variable and creates some output. ¤ PHP scripts MUST run on the server. ¤ They CANNOT be run using preview in browser.

slide-17
SLIDE 17

PHP Arrays

¤ Like variables, array names start with $ in PHP $scores = array(75, 65, 85, 90); ¤ As in Javascript, the index starts at [0] ¤ You can change values in an array $scores[3] = 95; ¤ Add another value to the end of the array $scores[] = 99; so $scores[4]=99

slide-18
SLIDE 18

Using Loops with Arrays

¤ Loops are used to iterate through arrays for ($i=0; $i < count($scores); $i++){ print ("$scores[$i] "); } ¤ This loop repeats 5 times with $i equal to 0, 1, 2, 3, and 4. ¤ This loop outputs: “75 65 85 95 99”

slide-19
SLIDE 19

foreach statement

¤ the foreach statement loops through the entire array sequentially ¤ Each time it loops through the $scores array, $item is set to the next item in the array

f

  • r

e a c h ( $ s c

  • r

e s a s $ i t e m ) { A r r a y N a m e I t e m v a r i a b l e ( $ i t e m ) i s a u t

  • m

a t i c a l l y s e t t

  • n

e x t a r r a y i t e m e a c h i t e r a t i

  • n

. S e t

  • f

s t a t e m e n t s t

  • r

e p e a t . }

slide-20
SLIDE 20

foreach statement

foreach ($scores as $item){ print "$item "; } ¤ The above outputs "75 65 85 95 99"

slide-21
SLIDE 21

$ m

  • n

t h s = a r r a y ( ' J a n ' = > 3 1 , ' F e b ' = > 2 8 , ' M a r ' = > 3 1 , ' A p r ' = > 3 , ' M a y ' = > 3 1 , ' J u n ' = > 3 , ' J u l ' = > 3 1 , ' A u g ' = > 3 1 , ' S e p ' = > 3 , ' O c t ' = > 3 1 , ' N

  • v

' = > 3 ) ;

N a m e

  • f

t h e a s s

  • c

i a t i v e a r r a y . I n d e x ' J a n ' a n d v a l u e 3 1 . I n d e x ' F e b ' a n d v a l u e 2 8 I n d e x ' M a r ' a n d v a l u e 3 1 .

Associative Arrays

¤ An associate array uses a key=>value pair ¤ Use the array() function along with the =>

  • perator to create an associative array.

¤ Here $months[‘Jun’]=30

slide-22
SLIDE 22

Associative Arrays

¤ The key is used to look up the value

$days = $months[‘Mar’];

¤ $days will be assigned the value 31. ¤ You can change a key’s value:

$months[‘Feb’] = 29;

¤ You can add an item:

$months[‘Dec’] = 31;

slide-23
SLIDE 23

Associative Arrays

¤ You can use foreach to access items from an associative array.

¤ Loops through each key=>value pair in the $months array ¤ The key is stored in $index ¤ The value is stored in $item

foreach($months as $index => $item)

{do something with the data}

slide-24
SLIDE 24

Associative Arrays

foreach ($months as $index => $item) {print "$index has $item days <br> ";} ¤ The above outputs:

Jan has 31 days Feb has 28 days Mar has 31 days Apr has 30 days May has 31 days Jun has 30 days . . . Dec has 31 days

slide-25
SLIDE 25

Associative Arrays

¤ Associative arrays can fetch data values only by using indices. ¤ You might be tempted to use a data item to fetch an index from an associative array, but it can’t be done. ¤ Indices are case sensitive.

slide-26
SLIDE 26

Debugging Arrays

¤ While debugging arrays you can’t just echo or

print out the entire contents of an array ¤ print_r(arrayname); is a quick way to

display the contents of an array.

slide-27
SLIDE 27

PHP Errors

Parse error: syntax error, unexpected T_ECHO in C:\htdocs\php\test.php on line 15 ¤ Ignore the “T” ¤ Fatal error: Often refers to a nonexistent file or function. ¤ Parse error: Usually a syntax error. ¤ Warning: Script will run, but there is something wrong. ¤ Notice: Script will run, but there is a minor issue.

Type of error What the error is Where the error took place

slide-28
SLIDE 28

PHP Scripts

¤ The source code of a PHP script is not visible. ¤ Save your php script as a .txt file for others to view the source code.

¤ .php runs the script ¤ .txt shows the contents of the script

slide-29
SLIDE 29

Lab

¤ Add an array to your lab. ¤ Write a loop that prints each value in the array. You can use a for or foreach loop, your choice.