+ All Categories
Home > Technology > Mdst 3559-02-15-php

Mdst 3559-02-15-php

Date post: 05-Dec-2014
Category:
Upload: rafael-alvarado
View: 472 times
Download: 0 times
Share this document with a friend
Description:
 
23
PHP MDST 3559: Dataesthetics Prof. Alvarado 02/15/2011
Transcript
Page 1: Mdst 3559-02-15-php

PHP

MDST 3559: DataestheticsProf. Alvarado

02/15/2011

Page 2: Mdst 3559-02-15-php

Business

• VPN?• Assignment grades will be posted in Collab

Page 3: Mdst 3559-02-15-php

Review

• JavaScript is a client-side language– Controls behavior of the document– Works on the document as a data structure

• jQuery is a JavaScript library– Makes working with JavaScript easier– Uses CSS selectors to access and manipulate

document elements

Page 4: Mdst 3559-02-15-php

jQuery Libraries

• jQuery – Core library, provides selectors and basic effects– http://jquery.com/

• jQuery UI– Provides user Interface functions, e.g. Tabs– http://jqueryui.com/ (See Demos)

• jQuery plugins– Hundreds of functions and effects– http://plugins.jquery.com/

Page 5: Mdst 3559-02-15-php

Another jQuery Example

• Create a class for notes that can be turned on an off

• Show jQuery features:– Manipulation: new text added– Traversing: moving between elements– Dynamic CSS: add styles dynamically to elements

Page 6: Mdst 3559-02-15-php

Overview

• Today we shift to PHP – a server-side programming language

Page 7: Mdst 3559-02-15-php

Client vs. Server Side

• All programs are human written sequences of commands that control the computer

• Programs are first “compiled” into a machine language

• When a program is “executed,” it produces an effect

Page 8: Mdst 3559-02-15-php

Client vs. Server Side

• In web programming, web pages can have either server-side or client-side programs embedded in them

• Server-side code gets executed before the page is delivered to the browser

• Client-side code gets executed after the page is delivered to the browser

Page 9: Mdst 3559-02-15-php
Page 10: Mdst 3559-02-15-php

PHP

• PHP is a simple server-side programming language

• It was invented in 1994 by Rasmus Lerdorf, a Danish Canadian, as a way to simplify the process of writing server-side programs– These older programs were called “CGI scripts”

• Originally PHP stood for “Personal Home Page,” now it just means PHP ...

Page 11: Mdst 3559-02-15-php
Page 12: Mdst 3559-02-15-php

Exercise 1: Hello, World!

• Create a new file, hello.php• Create an H1 Element called “Introduction to

PHP”• Create an H2 Element called “Exercise 1”• Enter the following code:

<p><?phpecho “Hello, World!”;?></p>

Page 13: Mdst 3559-02-15-php

Observations

• PHP files must end in .php– Otherwise, not executed, treated as text

• PHP code must live inside of the opening and closing “tags” <?php and ?>– This looks like XML but it is not– Anything outside of these tags is regular content, e.g. plain

text, HTML, or XML• echo is just a function that prints its argument to the

screen – echo does note require parantheses

• Your browser knows nothing about the PHP

Page 14: Mdst 3559-02-15-php

Exercise 2: Variables

• Comment out your first echo statement• Define a variable to hold the string “Hello,

World!”• Use echo to print the variable to the screen

Page 15: Mdst 3559-02-15-php

Variables

• Always prefixed by $• Variables hold values• VariableS are assigned values with the = sign– Variables are always on the left

Page 16: Mdst 3559-02-15-php

Exercise 3: Variable Types

• Comment out your last echo statement• Create a new variable called $notd (for “number

of the day”) and give it a numeric value, e.g.$notd = (42 * 5) + 29;– Do not use quotes

• Append text to your message variable$message .= “The message of the day is: ”;

• Then echo the message and the notd togetherecho “$message $notd”;

Page 17: Mdst 3559-02-15-php

Observations

• Values can be strings, numbers, or other things

• Strings are always quoted (in single or double quotes)

• Numbers are never quoted• Number can be the result of an equations– Operators: + - * / (also: ^ and %)

Page 18: Mdst 3559-02-15-php

Exercise 4: Arrays

• Create a new file rex.php• Invoke the file() function to grab a file from

the web and assign the return value to the variable $contentsUse the URL from post [8.1] on the blog$url = '.http://classics.mit.edu/Sophocles/oedipus.pl.txt’;

$contents = get($url);

• Invoke echo to print out $contents to screen

Page 19: Mdst 3559-02-15-php

Observation

• What do we see?– PHP is telling us that $contents is an array, not a

string• What is an array?

Page 20: Mdst 3559-02-15-php

Arrays

• Arrays are built-in data structures in the PHP language– A data type that holds other data types (strings,

numbers, other arrays)• Each stores a list of values– The items in the list are indexed a number starting

with 0• Let’s look back at our example ...

Page 21: Mdst 3559-02-15-php

Exercise 5

• Iterate through the $contents array using the foreach() function

• Then iterate through the $contents array using the for() function

Page 22: Mdst 3559-02-15-php

Observations

• Arrays have a length, or size• The size is always 1 greater than the last

index value– Since the index begins with “0”

• Iterating through arrays allows you to modify each item in the array

• Text files are often represented as arrays

Page 23: Mdst 3559-02-15-php

Exercise 6

• Using the foreach() function, format each line– Skip the first 5 lines– Remove the trailing new line– wrap each $line in paragraph tags


Recommended