PHP 01 - Daniel Sauter

advertisement

PHP 01

Introduction

Syntax

Variables

Keywords

Data Types

Relational Operators

Conditional Statements

Processing Forms for and while loops security

Based on Rasmus Lerdorf& Kevin Tatroe: Programming PHP. Sebastopol: O’Reilly, 2002.

W05/161B/Daniel Sauter

What does PHP do?

Simple yet powerful language designed for creating dynamic web content .

Mainly used to output HTML , however, any document format can be generated (supports PDF files, GIF, JPG, and PNG images, Flash movies, XML)

One of PHP’s main features is its wide-ranging database support

(e.g. MySQL , PostgreSQL, Oracle, Sybase, ODBC) note: this class will focus on the use of PHP to output HTML/XML and MySQL creating dynamic web content

History

Rasmus Lerdorf first conceived PHP in 1994 as Personal

Home Page Tools (PHP Tools)

Originally written for a project at the University of Toronto to represent data from various places in a web interface with PHP 3.0 it developed from a one-person effort to an open source project with developers around the world

PHP3 supported all major operating systems, databases and developed to a complete scripting language.

In Dec. 2005, 22,172,983 Domains, 1,277,375 IP Addresses used PHP

Usage until 2006

TIOBE Programming Community Index

The TIOBE Programming

Community index gives an indication of the popularity of programming languages.

The ratings are based on the world-wide availability of skilled engineers, courses and third party vendors.

The popular search engines

Google, MSN, and Yahoo! are used to calculate the ratings.

Observe that the TPC index is not about the best programming language or the language in which most lines of code have been written.

Source: http://www.tiobe.com/ tpci.htm, checked Feb. 17 2006

Language Basics

PHP is strongly influenced by other programming languages, therefore the concepts behind the lexical structure of PHP can be easily applied to other languages, e.g. ActionScript, Java or

Processing.

PHP is not case sensitive, all three lines are equivalent: echo(”hello!”);

ECHO(”hello!”);

EcHo(”hello!”);

As in HTML, whitespace doesn’t matter in a PHP program, you can spread statements across any number of lines or put them in a single line.

Syntax

Elements of the language and how they are structured.

- Statement

- Statement terminator

- Function (Command)

- Parameter

- Comments

// Program to write my name

$name = "Daniel"; echo($name);

Variables

- are used to store values

- always begin with a dollar sign ($)

- have a name/identifier and a value

- are case-sensitive

- must not begin with special characters or number, no space.

$name = "Daniel"; // Declare and assign

$number = 32; // Declare and assign

$counter = 12; // Assign variable echo($number); echo($name); echo($counter);

Scope of Variables

The scope of variables determines which parts of the program can access it. The scope changes depending on the location of the variable’s declaration. There are four types of variable scope in PHP:

- global (can be accessed anywhere in the program)

- local (as part of a function)

- static (retains its value between calls of a function)

- function parameter (only accessible inside the function)

We’ll start by using predominantly global variables.

Keywords

A keyword is a word reserved my the PHP programming language for its core functionality. You cannot give a variable, function, or class name the same name as a keyword.

Selected keywords are for example: and, not, or do, for, while if, else, elseif, switch new true, false eval return

...

Data Types of Variables (selected)

20038, -33, 2 // Integer, whole numbers

3.1254, 0.1 //Floating Point, numeric values with decimal digits

‘hot dog’, “Daniel” // String, sequence of characters false, true, 0, “” // Boolean, “truth value” of conditional statement

$person = array(’Jeff’, ‘Daniel’, ‘you’); // Array, group of values

$person[0] = “ Jeff”;

$person[1] = “Daniel”;

$person[2]= “you”;

Relational Operators

Used to compare values:

> (greater than)

< (less than)

>= (greater than or equal to)

<= (less than or equal to)

!= (inequality)

== (equality)

Relational Operators

5 > 4 // True

5 < 3 // False

5 > 5 // False

5 >= 5 // True

5 >= 6 // False

5 != 5 // False(not equal)

5 == 5 // True

5 == 4 // False

Conditional Statements

Used to make decisions about which code to execute and which to ignore. E.g.:

$num = 5; // assigning the value if($num < 10) { // if condition is true echo("Less than 10"); // this line is executed

}

$num = 5; if($num < 10) { echo("Less than 10");

} else if($num > 10) { echo("More than 10");

} else { echo("Equal to 10");

}

note: in order to understand PHP quickly and effectively, a good understanding of HTML is required.

Because PHP and HTML code is mixed within the Code, knowing exactly what belongs to which language is essential

This is increasingly important in the process when JavaScript or CSS elements are added

Please continuously refer to Web Design in a Nutshell (Jennifer

Niederst) to revisit concepts of HTML, JavaScript and CSS if required.

Echo

Use echo to put a string into the HTML code of a PHP generated page. Both statements are equivalent.

echo “A message for the browser”; echo (”A message for the browser”);

Echo’ing a variable echo $variable;

Echo looks and behaves like a function, in fact it is a language construct, which means that you can omit the parentheses which all functions need.

Embedding PHP in HTML

process.php:

<html>

<body>

<?php

$birthday = $_POST['birthdate'];

?> echo "Your birthday: $birthday";

</body>

</html>

Processing Form Data

input.html:

<html>

<body>

<form action="process.php" method="post">

<input type="text" name=" birthdate ">

<input type="submit" value="send birthdate">

</form>

</body>

</html> process.php:

<html>

<body>

<?php

$birthday = $_POST['birthdate']; echo "Your birthday: $birthday";

?>

</body>

</html>

Workshop Exercises

Using PHP:

1. Using PHP, Write ‘hello world’

2. Make a form with one input field and a submit button. After submitting, write the content of this input field on the screen, and, the input field as mentioned before.

3. Use the html document you created earlier (including a popup menu, a checkbox, a text area, etc. Write all contents/ values of these forms onto the screen.

PHP ‘for’ loops

Controls a sequence of repetitions. A for() structure has three parts: init , test , and update . Each part must be separated by a semi-colon ";". The loop continues until the test evaluates to false.

When a for() structure is executed, the following sequence of events occurs:

1. The init statement is executed

2. The test is evaluated to be true or false

3. If the test is true, jump to step 4. If the test is False, jump to step 6

4. Execute the statements within the block

5. Execute the update statement and jump to step 2

6. Exit the loop.

for ($i = 1; $i <= 10; $i++) {

echo $i;

}

PHP ‘while’ loops

The while structure executes a series of statements continuously while the expression is true. The expression must be updated during the repetitions or the program will never "break out" of while().

$i = 1; while ($i <= 10) {

echo $i++;

}

Workshop Exercises

Using HTML and PHP:

1. Create an html page including a form and a text filed, name it amount.html

2. based on the value provided in amount.html, create a for loop which prints the variable’s value and a text message as many times as the value submitted by the user.

Register_Globals on/off

If the ‘register_globals’ option in the PHP setup is activated

(php.ini), PHP creates a separate global variable for every form parameter.

This functionality is both convenient and dangerous, as it lets the browser provide initial values for those variables, which makes the system vulnerable to attacks and it presents a security problem.

Security: register_globals

Download