Date post: | 21-Dec-2015 |
Category: |
Documents |
Upload: | marjorie-nash |
View: | 223 times |
Download: | 0 times |
ITC 240: Web Application ProgrammingSUBHASH PRAJAPAT I
06 /29 /15
MSE, SEATTLE UN IVERS ITY
Course Overview• Course Description, Syllabus and Assignments available on Canvas
• Major Resource:o No particular Text book oModules on Canvas as our guideo Class Notes/ Exercises, Slideso Selected Videos/Article Links
• References:o “PHP Solutions: Dynamic Web Design Made Easy” by David Powers (Apress)o Programming PHP – O’Reillyo Or any other beginning PHP books
Course Overview• Grading:o On-class exerciseso AssignmentsoQuizo Project
• Contact oMessage through Canvaso Office Hours: by appointmentso Email: [email protected], [email protected]
Today’s Class• Web Programming
• Client Server Architecture
• Introduction to PHP
• Client Side vs Server Side Language
• How PHP works & PHP Fundamentals
Client Server Architecture
Closer Look: Client Server Architecture
PHP• Server Side Programming Language for dynamic websites
• Open Source Language
• Initially released as Personal Home Page (PHP) Tool
• Now stands for Hypertext Preprocessor
• Originally developed by Rasmus Lerdorf in 1995 and maintained by PHP team.
• Current Release: 5.6.10 (June 11, 2015)
• Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
• Compatible with almost all servers used today (Apache, IIS, etc.)
• Supports a wide range of databases
Popularity PHP is used by 82.0% of all the websites whose server-side programming language we know.
Source: http://w3techs.com/technologies/details/pl-php/all/all
Sites using PHP
• Facebook.com
• Baidu.com
• Wikipedia.org
• Twitter.com
• Tmall.com
• Flickr, Yahoo, Tumblr
Open Source Tools: Wordpress, Drupal, Magento, Joomla, Wiki etc.
What PHP can do?• Generate dynamic page content
• Command Line Scripting
• Create, open, read, write, delete, and close files (text, image etc.) on the server
• Collect form, validate and store form data
• Send and receive cookies, Send emails
• Add, delete, modify data in your database
• Can encrypt data
• Can be used to control user-access
Class Exercise – 1.1hello.html
<html>
<head><title>PHP Test</title></head>
<body>
<p>Hello World</p>
</body>
</html>
Replace the highlighted line with following and save file as hello.php
<?php echo '<p>Hello World</p>'; ?>
Upload both files (hello.html and hello.php on web server and see the page/view source)
HTML Source
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
PHP Syntax
• PHP files have .php extension
• A PHP script starts with <?php and ends with ?>
• A PHP script can be placed anywhere in the document.
• A PHP file normally contains HTML tags, and some PHP scripting code.
• PHP statements end with a semicolon (;)
• PHP Comments
// Single Line Comment
/*
multiple line comment
*/
HTML/JavaScript/PHP HTML
<p>Hello World</p>
JavaScript
<script>
document.write ('<p>Hello World</p>');
</script>
PHP
<?php
echo '<p>Hello World</p>';
?>
So, What is the difference?
Client Side/Server Side Language
JavaScript: Client Side Language - executes on client side
PHP: Server Side Language - executes on server side, sends result to client
<script>
document.write ('<p>2+3 is '+ (2+3) +'</p>');
</script>
<?php
echo '<p>2+3 is '.(2+3).'</p>';
?>
Both will print: 2+3 is 5 . What is the difference?
Quiz JavaScript
<script>
var d = new Date();
document.write (d.toString());
</script>
PHP
<?php
echo date ('Y/m/d H:i:s');
?>
Q: What will be the output? Why?
PHP Fundamentals PHP & Text
<div align="center"> <?php echo "Place some PHP here!"; ?> </div>
Interspersing PHP & HTML
<div align="center"> <?php print "<b>This is bold</b>"; ?> </div>
Escaping Character
<?php echo 'We have a customer named O\'Malley'; ?>
PHP Fundamentals Single vs Double Quotes
With a lot of HTML, we could use single quotes since HTML attributes use lot of double quotes
<?php echo '<div align="center"><font color="red">This has double quotes!</font></div>'; ?>
We could use double quotes with SQL which identifies text (string) data with single quotes.
PHP FundamentalsStrings
A sequence of characters, like "Hello world!“
<?php echo “hello world”; ?>
String Concatenation
<?php echo “hello”.”world”; ?>
Arithmetic Operators Operation Symbol Example
Negation - -5
Addition + 3+5
Subtraction - 7-4
Multiplication * 5*6
Division / 10/3
Modulus (remainder) % 10%3
Class Exercise – 1.2PHP Arithmetic Operation
<?php
echo "5+2 is ".(5+2);
echo “<br>”;
echo "5-2 is ".(5-2);
echo “<br>”;
echo "5*2 is ".(5*2);
echo “<br>”;
echo "5/2 is ".(5/2);
echo “<br>”;
echo "5%2 is ".(5%2);
echo “<br>”;
?>
Discussion
What would you do if you need to change all the 5s to 10 and all the 2s to 5?
Variables• Storing data into memory location.
• Starts with $ sign
• Can be a single character, multiple characters, numbers etc.
Eg:
<?php
$myName = “Lisa”;
echo $myName;
$a = 5;
$b = 6;
echo $a+$b;
?>
VariablesSingle quote/ Double quote
values enclosed within single quotation marks will be treated literally, whereas those within double quotation marks will be interpreted
<?
$myNum = 1;
$myOtherNum = 3;
$myTotal = $myNum + $myOtherNum;
echo "You added $myNum and $myOtherNum and the total is $myTotal";
?>
Class Exercise – 1.3 Rewrite the Exercise 1.2 with using variables instead of 5 and 2
For eg:
<?php
$firstNumber = 5;
$secondNumber = 2;
echo “$firstNumber+$secondNumber is ".($firstNumber+$secondNumber);
.
.
?>
Discussion Instead of 5 & 2, we wanted to do calculation of 20 & 10, 40 & 30, 20 & 5 etc.
What would you do?
Function Function is a block of code that performs certain task
<?php
function printName (){ echo “my name is Linda”;
}
printName ();
?>