oktober 2002bent thomsen - fit 1-11 it – som værktøj bent thomsen institut for datalogi aalborg...

35
Oktober 2002 Bent Thomsen - FIT 1-1 1 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Upload: sabrina-ward

Post on 04-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Oktober 2002 Bent Thomsen - FIT 1-1 1

IT – som værktøj

Bent Thomsen

Institut for Datalogi

Aalborg Universitet

Page 2: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Introduction to PHP

Bent Thomsen

Page 3: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

What is PHP?

• PHP is a generalized Open Source Server Side Scripting Language that generates HTML content

• PHP: Recursive Acronym for – PHP: Hypertext Preprocessor

• PHP was created by Rasmus Lerdorf in 1994 as a tool for Web Development

• PHP is simple for beginners, yet a powerful tool for the professional Web Developer

Page 4: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Embedding PHP in HTML is different from writing a script in other languages like Perl, but similar to JavaScript

Instead of writing a program with lots of commands to output HTML, you write an HTML script with some embedded code to do something (in the example on the next page, to output some text).

The PHP code is enclosed in start and end tags that allow you to jump into and out of "PHP mode".

An example follows

Page 5: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Embedding PHP in HTML

<html><head>

<title>Example</title> </head>

<body> <?php echo “Hello World!"; ?>

</body> </html>

Page 6: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

HTML Embedding

– SGML Style: <? echo “Hello World!”; ?>

– XML Style: <?php echo “Hello World!”; ?>

– ASP Style: <%= “Hello World!”; %>

– JavaScript Style:

<script language=“php”>echo “Hello World!”;</script>

Page 7: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

PHP vs. JavaScript• What distinguishes PHP from client-side

JavaScript?

• PHP code is executed on the server.

• If you were to have a script similar to the above on your server, the client would receive the results of running that script, with no way to determine what the underlying code may be.

Page 8: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Client-side execution using JavaScript

Web-ServerJavaScript

WWWWeb-Browser

HTML Page...<SCRIPT ...>...

Web-Client

Page 9: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Server-side execution using PHP ScriptServer-side execution using PHP Script

Web-Client Web-Server

PHPScript

HTML-Form SubmitForm Data

Call PHPinterpreter

Response Response

Web-Browser WWW

Reply

Page 10: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

PHP & Web ServersPHP supports most web servers including•Apache•Microsoft Internet Information Server• Personal Web Server•Netscape and iPlanet servers•Oreilly Website Pro server•Caudium, Xitami, OmniHTTPd

For most servers PHP has a module, for the others supporting the CGI standard, PHP can work as a CGI processor.

Page 11: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

PHP VARIABLES

• PHP keeps variables easy to work with• Syntax is minimal• All PHP variables start with $• Variable names are case sensitive• Strings, integers, floating-point, arrays• Variables generally come from 3 places

– Assigned within a script– Passed from an HTML page– Or from the PHP environment

Page 12: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

PHP VARIABLES

• PHP does not require explicit variable declarations

• Use a variable, and it exists!• Examples of variable declaration in PHP

– $a = “this is a string”; //this is a string– $b = 4; //this is an integer– $c = 3.1415; //this is a floating point number– $d = “5”; //this is another string

Page 13: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

PHP Is Flexible

• $g = $b + $d ; echo $g will print 9 (See Preceding Slide)

$m = 7; // $m is an integer

$n = 1.732; // $n is double floating point

$p = $m + $n; //$p is also a double floating point variable

Page 14: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

The = sign

• Use = to indicate an assignment statement:– $X = 4;

• Use = = for comparison:– If($X==4);

Page 15: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

More on Strings

• $my_name = “Wendel”;• $statement = “Hello, my name is $my_name”;

– This expands the variable $my_name

• So the Echo $statement;

Will print, “Hello, my name is Wendel”

If you want to include any of the following characters in your string you must first escape each with backslashes “ \ $

Page 16: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Operators

• Assignment (=) – returns a value equal to the value that was assigned: $a = ($b = 5);

• Arithmetic: + - * \ %• Concatenation: “now”.“one String”• Comparison: == != > >= < <=

• Logical: || or xor && and !

Page 17: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Operator Presedence

• (cast)• * \ %• + -• < <= > >=• == !=• &&• ||• = • and• xor• or

Can be changed by using parentheses

e.g.

2*5+6

2*(5+6)

Page 18: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Constants

• You indicate that a label is to represent a constant by:

define(‘CONSTANT_NAME’,value) ordefine(‘CONSTANT_NAME’,value,true)

• When the third parameter is present and true then the parameter label can be used in either upper or lower case

e. g.

define(‘PI’,3.1412,true)

defines a constant PI and another pi that both have the value 3.1412

Page 19: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Statements• Function calls: print(“hello”);• Assignment statements: $first = “first one”;• If/Then/Else statement:

if (expression){ true_part } else { false_part }

• While statement: while (expression){ repeated_part }

• For statement: for (init; test; update){ repeated_part }

Page 20: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

If/Then/Else

• If/Then/Else statement:if (expression){ true_part }

else { false_part } • Used to conditional execute part of a PhP script• The else part is optional• Example:if ($num == 1) { $end = “st”; }else if ($num == 2 or $num ==3) { $end = “ed”; }else { $end = “th”; }print (((string)$num).$end);

Page 21: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

While

• While statement: while (expression){ repeated_part }

• Loop, that is repeat all the enclosed PhP statements, while the condition is true

• Example:$count = 10;while ($count > 0){ print(“the count is $count <br>”);

$count = $count – 1;}

Page 22: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

For• For statement:

for (init; test; update){ repeated_part }

• Execute the init statement, then test the condition; then repeat the loop

• In the loop, as long as the condition is true, execute the statements in the body; then execute the update statement; then test the condition

• Example: for ($n = 1; $n < 10; $n = $n + 1){ print (“the count is $n <br>”);}

Page 23: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Array Examples

• Creating a simple Array$class = array (“Web Programming II”, “CS463”, “MWF”);

• Creating an Associative Array$ranks ( “first” =>1,

“second” =>2,“third” =>3 );

• Heterogeneous Array$studentA = array ( “StudentA”, “Sr”, 3.4, 103);

• Printingecho class[2]; // echo out the second element: prints “CS463”;echo ranks[3]; // echo out the third element: prints 3;echo studentA[4]; // echo out the cumulative credits: prints 103;

Page 24: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Array - multidimensional• creating

$students = array( array( “StudentA”, “FR”, 3.0),array( “StudentB”, “JR”, 2.8),array( “StudentC”, “SR”, 3.3) );

• Printingprint $student[2][0]; //prints “StudentC”

• More sophisticated?$students2 = array(

“StudentA” => array( “Year”=>”FR”, “GPA”=>3.0),“StudentB” => array( “Year”=>”JR”, “GPA”=>2.8),“StudentC” => array( “Year”=>”SR”, “GPA”=>3.3) );

• Printingprint $students2[“StudentB”][“GPA”]; // prints 2.8

Page 25: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Functions

• Functions denote actions that can be taken (on your behalf) by php

• Example

print(“This is some text”);• Functions can be parts of an expression

• Functions have one or more arguments (the part inside the parentheses)

• The values of the arguments are past to the functions that do something with them (“side effects”)

• A Function may return a value (example shortly)

Page 26: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

More Functions

• Built-in functions: – print, round, sqrt, cos, rand, …

• Use defined functionsfunction diff($par1,$par2){

if ($par1 > $par2){return $par1 - $par2;

} else {return $par2 - $par1;

}}

Page 27: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Commenting in PHP

• /* */

• // to the end of the line

Page 28: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

An example

<?php/* First define functions

use them later */function diff($par1,$par2){

if ($par1 > $par2){return $par1 - $par2;

} else {return $par2 - $par1;

}}

echo “The difference between 5 and 10 is”;echo diff(5,10); ?>

Page 29: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Form Handling

• A simple form:

<form action="simple_form.php" method="POST">

Your name: <input type=“text” name=“name”><br/>

You age: <input type=“text” name=“age”><br/>

<input type=“submit”/>

</form>

• The form handling code:

Hi <?php echo $name; ?>.

You are <?php echo $age; ?> years old.

Page 30: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Form Handling

• Magic Global Variables, $HTTP_GET_VARS, $_GET,

register_globals configuration

Hi <?php echo $HTTP_GET_VARS[‘name’]; ?>.

You are <?php echo $_GET[‘age’]; ?> years old.

Page 31: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

File Access

• Local File Access

– fopen, fread, fwrite, fclose, fputs, freads, feof, much more…

• Remote File Access

– Uses the same functions as local file access

– Uses URL’s to retrieve files, FTP and HTTP supported.

<?php readfile(‘http://www.ActiveState.com/’); ?>

– Can write files to FTP is username and password is sent

• ftp://username:[email protected]/path/filename

Page 32: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Authentication<?

function authenticate() {

global $PHP_AUTH_USER; global $PHP_AUTH_PW;

if(!($PHP_AUTH_USER == “user" && $PHP_AUTH_PW == “password“)) {

Header(‘WWW-Authenticate: basic realm=“My Website“’);

Header(‘HTTP/1.0 401 Unauthorized’);

echo(‘Please enter a username and password to proceed.’);

return false;

}

return true;

}

If (!authenticate()) exit;

echo “You have authenticated properly!”;

?>

Page 33: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

SQL Databases• Wide range of SQL database supported

– MySQL, Postgress, MS-SQL, Oracle, Sybase, ODBC, DBM, Informix…

– Native interfaces (MySQL, etc), and abstracted interfaces (ODBC, dba,

PEAR)

– Persistent connections supported

<?php

$conn = mysql_pconnect(“localhost”, “username”, “password);

mysql_select_db(“mydatabase”, $conn);

$res = mysql_query($conn, “SELECT * FROM resources”);

while (($rs = mysql_fetch_array($res))) {

echo(“column1: “.$rs[0].” column2: “.$rs[1].” …<br>\n”);

}

mysql_close();

?>

Page 34: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Putting it all togetherWeb-Client

Web-Server

DBMS

DatabaseOutput

SQL commands

PHPScript

HTML-Form (+JavaScript)

Reply

WWW

SubmitData

Call PHPinterpreter

Response Response

LAN

Web-Browser

DatabaseServer

Page 35: Oktober 2002Bent Thomsen - FIT 1-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

What have we covered?

• A bit about IT history• Operating systems (Unix)• Some (hopefully) useful applications

– Browsers, IM, text processing (LaTeX)• Networks and the Internet (HTML)• Systems for engineering calculations

– MATLAB and Excel• Database systems

– Excel and Access• Programming (JavaScript and PHP)