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 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
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
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
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
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
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
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
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, omit the quotation marks. ¤ print $x*2; ¤ 20
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.
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
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>';
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 */
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.
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
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.
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
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”
foreach statement ¤ the foreach statement loops through the entire array sequentially 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 o m a t i c a l l y s e t t o f o r e a c h ( $ s c o r e s a s $ i t e m ) { n e x t a r r a y i t e m S e t o f s t a t e m e n t s t o r e p e a t . e a c h i t e r a t i o n . } ¤ Each time it loops through the $scores array, $item is set to the next item in the array
foreach statement foreach ($scores as $item){ print "$item "; } ¤ The above outputs "75 65 85 95 99"
Associative Arrays ¤ An associate array uses a key=>value pair ¤ Use the array() function along with the => operator to create an associative array. ¤ Here $months[‘Jun’]=30 N a m e o f t h e a s s o 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 3 1 . I n d e x ' M a r ' a n d v a l u e $ m o 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 0 , ' M a y ' = > 3 1 , ' J u n ' = > 3 0 , ' J u l ' = > 3 1 , ' A u g ' = > 3 1 , ' S e p ' = > 3 0 , ' O c t ' = > 3 1 , ' N o v ' = > 3 0 ) ;
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;
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}
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
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.
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.
PHP Errors Type of error What the error is Parse error: syntax error, unexpected T_ECHO in C:\htdocs\php\test.php on line 15 ¤ Ignore the “ T ” Where the error took place ¤ 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.
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
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.
Recommend
More recommend