Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
INTRODUCTION TO PHP
Topic to discussed
1. 2. 3. 4. 5. 6. 7. 8. 9. Basic Syntax Sending Data to the Web Browser Understanding PHP, HTML and White Space Writing Comments What are Variables? About Strings About Numbers About Constants Single vs. Double Quotation Marks
Dynamic Pages
As the web evolved, sites became more complex and were expected to display dynamic information which could not have been hard coded in HTML:
search results number of visitors user login message boards
What is PHP?
PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software (OSS) PHP is free to download and use
What is PHP?
PHP: Hypertext Preprocessor
An HTML embedded scripting language Use to develop dynamic web site
Better, faster and easier to learn Designed to do something only after event occurs. e.g. when user submit forms
A server-side language, the code resides on a host computer that serves Web pages to the requesting visitors (client).
URL request client HTML
server
HTML PHP Script request
What is MySQL?
DBMS for relational databases (RDBMS) A database being a collection of interrelated data, for text, numbers or binary files. Open source database
URL request client server HTML HTML PHP Script request Query Data MySQL
Why PHP?
PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side
Development Tools
Web server application Apache or IIS PHP MySQL Web browser mozilla, internet explorer Text editor, PHP capable applicationMacromedia Dreamweaver,IDE
Basic syntax
PHP is HTML embedded scripting language PHP code is enclosed with PHP tags written in formal or informal style
<?php . ?>
formal (XML style)
<? . ?>
informal
Intermingle PHP and HTML code within the same script <html> <title>Basic PHP </title> <? //comment write php code here ?> </html>
Basic syntax
To place PHP code within HTML document, use
<?php ?>
http://localhost/filename.php
For example, <?php echo This was done using PHP; ?> Will display: This was done using PHP
PHP is generally white space insensitive, meaning that you can space out your code to make your scripts more legible.
Variables
PHP syntactical rules for creating variables:
1.
2.
3.
4. 5.
A variables name (identifier) must start with a dollar sign ($), for example $name Can contain combination of strings, numbers & underscore ($my_report1) First character after dollar sign ($) must be either a letter or an underscore (it cannot be a number) Variable names in PHP are case-sensitive. Variables can be assigned using the equal sign (=) to print out PHPs predefined variables
/*
$_SERVER refers to mass of server-related information, like the name of the script being run ($_SERVER['PHP_SELF']), the Web browser and operating system of the user accessing the script ($_SERVER['HTTP_USER_AGENT']) and the IP address of the user accessing the script ($_SERVER['REMOTE_ADDR']) */
String
A quoted chunk of letters, numbers, spaces, punctuation .. Example strings hello software 1000 12 January, 2006 String variable assign a string value to valid variable name $today =16 July, 2007; To print out echo $today; echo Today is $today; Concatenation string Addition of strings using period (.). $day=12; $month=January; $year =2006; $today = $day . . $month . . $year Use it extensively when building database queries in later chapters
Variables: Strings
A string is a quoted chunk of letters, numbers, spaces, punctuations, etc. E.g.
Ali In my mind 1000 9 july 1978
Strings are case sensetive so $Welcome_Text is not the same as $welcome_text When assigning numbers to strings you do not need to include the quotes so: $user_id = 987 would be allowed.
Outputting Variables
To display a variable on the screen uses exactly the same code as to display text but in a slightly different form. The following code would display your welcome text: <? $welcome_text = "Hello and welcome to my website."; print($welcome_text); ?> As you can see, the only major difference is that you do not need the quotation marks if you are printing a variable. String variables are created and their values sent to the Web browser in this script
Concatenating strings
$city = Ipoh; $state = Perak
$address = $city . $state ; Will display: IpohPerak To improve: $address = $city . , . $state ;
Add a comma and a space
About Numbers
Functions round() $j = 3.14; $k = round( $j); number_format() $p =20980; $g=number_format($p); $g=number_format($p,2);
Using numbers
Valid number-type variables can be like: 8, 3.14, 109080808, -4.524 To format the number into thousands and round it to two decimal places: $total = number_format ($total, 2);
Numbers
$n = 3.14; $n = round ($n); // 3 rounds a decimal to the nearest integer $n = 3.142857; $n = round ($n, 3); // 3.143 rounds to specified number of decimal places $n = 20943; $n = number_format ($n); // 20, 943 grouped into thousands using commas $n = 20943; $n = number_format ($n, 2); // 20, 943.00 set a specified number of decimal points
Arithmetic operators
+ addition - subtraction * multiplication / division % modular ++ increment -- decrement
Constants
Specific data type Retain initial value throughout script Cannot change once it has been set Use define() define (AGE, value); Print constant echo Hello, . AGE; OR echo Hello,, AGE;
Single quote -> values treated literally Double quote -> interpolated Example
$var =Hello; echo var equal to $var; echo var equal to $var; echo \$var is equal to $var;
=> replace variables name with its value and a special characters code (\$) with its represented value => display exactly what you type, except for the escaped single quote (\) and the escape backslash(\\).
cont
Exercises
Give an example of static web site that you know. Discuss what are the advantages and disadvantages of static website.