PHP and JavaScript Programming - Towson University

advertisement
PHP and JavaScript
Nov. 26, 2015
Kyung Eun Park & Hojin Chang
Computer and Creativity (COSC109)
Towson University
Table of Contents
1. Web Architecture
2. PHP
3. JavaScript
4. XAMPP
2
Web Architecture 1/2
 2 Tier
 Client (Web Browser)
 Web Server
 http://triton.towson.edu/~karne/
 3 Tier




Client (Web Browser)
Middle (Web Server & Servlet Objects)
Database System
http://pjd.mscui.net/PrimaryCare.htm
Web Architecture 2/2
 3 Tier Examples
Syntax
 prescribes the ways in which statements must be
written in order for them to be understood by the
computer
 like the rules of grammar and punctuation in human
languages, but these rules must be followed
precisely in computer programming
5
Variables
 Purpose:
to store values that can be updated and retrieved at
runtime
 Data is stored in memory as bits.
 Variable lets you refer, by name, to the data's memory
location stored.
6
PHP
 The language that you use to make the server generate
dynamic output.
 PHP documents end with the extension .php.
 A Web server passes a PHP program to the PHP
processor.
 A new tag to trigger the PHP commands:
 <?php
…
?>
<?php
echo “Hello World”;
?>
7
The Structure of PHP
 Looks more like Java, but flexible language
 Semicolons: PHP commands ends with a semicolon
 $ symbol: placed in front of all variables
<?php
$mycounter = 1;
$mystring = “Hello”;
$myarray = array(“One”, “Two”, “Three”);
?>
8
PHP Variables (1)
 String variables
<?php
$username = “Tom Sawyer”;
echo $username;
?>
 Numeric variables
<?php
$count = 17;
echo $count;
?>
9
PHP Variables (2)
 Array variables
<?php
$team= array(“Bill”, “Joe”, “Mike”, “Chris”, “Jim”);
echo $team[3];
?>
10
PHP Operators
 Arithmetic operators: +, -, *, /, %, ++, - Assignment operators: =, +=, -=, *=, /=, .=, %=
 Comparison operators: ==, !=, >, <, >=, <=
 Logical operators: &&, and, ||, or, !, xor
11
PHP Variable operations
 Assignment
$x = 10;
$+=10;
$y =20;
$y -=10;
 Variable incrementing and decrementing
++$x;
--$y;
12
PHP String
 String concatenation
echo “You have ”.$msgs.“ messages.”;
$bulletin .= $newsflash;
 Variable substitution
echo “There have been $count presidents of the US.”;
 Escaping characters
$text = “My mother always said \”Eat your greens\”.”;
13
Multiline String Variable
 Multiple lines between quotes
 <<< operator
<?php
$author = “Alfred E Newman”;
$out = <<<_END
This is a Headline
This is the first line.
This is the second.
- Written by $author.
_End;
?>
14
PHP Variable Typing
 PHP is a very loosely typed language.
 No need to declare variables before they are used.
 Context-based variable typing by PHP if necessary.
<?php
$number = 12345 * 67890;
echo substr($number, 3, 1);
?>
<?php
$pi = “3.1415927”;
$radius = 5;
echo $pi * ($radius * $radius);
?>
15
PHP Constants
 Similar to variables, but its value is set for the
remainder of the program and cannot be altered.
define(“ROOT_LOCATION”, “/usr/local/www/”);
$directory = ROOT_LOCATION;
 Predefined constants
__LINE__, __FILE__, __DIR__, __FUNCTION__,
__CLASS__, __METHOD__, __NAMESPACE__
16
PHP Function
 To separate out sections of code that perform a
particular task.
<?php
$temp = “The date is “;
echo $temp.longdate(time());
function longdate($timestamp) {
return date(“ 1 F jS Y”, $timestamp);
}
?>
17
JavaScript
 Client-side scripting language that runs entirely inside the
web browser
 Placed between opening <script> and closing </script>
HTML tags
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<script>
document.write(“<p>My First JavaScript</p>”);
</script>
</body>
</html>
18
JavaScript Function
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
</body>
</html>
19
JavaScript Variables
 String variables
greeting = “hello there”
newstring = greeting
document.writing(newstring)
 Numeric Variables
count = 42
temperature = 100.0
 Arrays
toys = [“bat”, “ball”, “whistle”, “puzzle”, “doll”]
document.write(toys[3]);
20
JavaScript Operators
 Arithmetic operators: +, -, *, /, %, ++, - Assignment operators: =, +=(string too), -=, *=, /=, %=
 Comparison operators: ==, !=, >, <, >=, <=, ===, !==
 Logical operators: &&, ||, !
 Incrementing and Decrementing operators: ++, - String concatenation: +
 Escaping Characters: \b, \n, \t, \’, \”, \\, etc.
21
JavaScript Typing
 Like PHP, JavaScript is a very loosely typed language
 Determined only when a value is assigned and can
change as the variable appears in different contexts
22
Installing XAMPP

Free and open source cross-platform Web server solution stack pagkage consisting of:
 The Apache HTTP Server
 MySQL database
 Interpreters for scripts written in the PHP and Perl programming language
 X(cross-platform), Apache HTTP Server, MySQL, PHP, Perl

Intended it for use only as a development tool
 Allow website designers and programmers to test their work on their own computers without

any access to the Internet.
In practice, however, XAMPP is sometimes used to actually serve web pages on the World
Wide Web.

Support for creating and manipulating database in MySQL and SQLite

Download at:
http://www.apachefriends.org/en/index.html
http://www.apachefriends.org/en/xampp.html
http://www.apachefriends.org/en/xampp-windows.html
http://en.wikipedia.org/wiki/XAMPP
23
XAMPP Control Panel
24
Running *.html and *.php files
25
localhost/index.php
26
localhost/index.html
27
Download