Upload
rafael-alvarado
View
479
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
PHP
MDST 3559: DataestheticsProf. Alvarado
02/15/2011
Business
• VPN?• Assignment grades will be posted in Collab
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
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/
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
Overview
• Today we shift to PHP – a server-side programming language
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
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
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 ...
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>
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
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
Variables
• Always prefixed by $• Variables hold values• VariableS are assigned values with the = sign– Variables are always on the left
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”;
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 %)
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
Observation
• What do we see?– PHP is telling us that $contents is an array, not a
string• What is an array?
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 ...
Exercise 5
• Iterate through the $contents array using the foreach() function
• Then iterate through the $contents array using the for() function
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
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