Professional Documents
Culture Documents
csc492
We require
Web Server (Apache) PHP Database Text Editor (Dreamweaver) Web Browser
Structure
What is PHP?
Server-side, scripting language
Event based
Provide more flexibility than HTML alone Syntax is similar to C, Java, Perl
PHP History
PHP succeeds an older product, named PHP/FI. PHP/FI was created by Rasmus Lerdorf in 1995. Personal Home Page/ Forms Interpreter - First Public release Version 3: 1997 PHP: Hypertext Preprocessor created by Andi Gutmans and Zeev Suraski Version 4: 2000 - Zend, full feature Actively supported by updates Version 4.4.6 on March 1, 2007 Version 5: 2004 New improved, Object Oriented & database connectivity The latest version Version 5.2.1 Stable
Powerful, Robust, scalable (Handle the traffic grow with you) Web development specific (Personal web site) Can be object oriented, especially in version 5 Great documentation in many languages
Documentation PHP?
http://www.php.net http://docs.php.net/manual/en/getting-started.php
Windows Installation
PHP requires:
Windows 98/Me or Windows NT/2000/XP/2003
Apache:
http://httpd.apache.org/
PHP:
http://www.php.net
MySQL:
Dev.mysql.com
WAMP:
www.wampserver.com/en/index.php
XAMPP:
www.apachefriends.org
Ill be using:
XAMPP 1.6.6a
Apache HTTPD 2.2.8 + Openssl 0.9.8g MySQL 5.0.51a PHP 5.2.5 PHP 4.4.8 phpMyAdmin 2.11.4
Text Editor
Dreamweaver, Zend Studio, NotePad Word processors are not good for PHP editing PHP designer Best editors have:
Syntax coloring/highlights Syntax hinting
PHP Programming
<?php ?>
Short syntax
Comments
It is good practice to enter comments
//single-line comments are like this #or like this /* Multiple line comments*/
Variables Names
In PHP variable starts with a $ Followed by letter or underscore Can contain letters, numbers, underscores, or dashes No spaces Case-sensitive $student and $Student are different as it is case sensitive Global variable global $student Superglobals Automatically available throughout all program $_GET $_POST $_Request $_COOKIES $_SESSION
Strings:
Strings concatenate:
Note: strings.php
Strings function:
Note: strings2.php
Basic Math:
Note: basic.php
Float
Float
IF statement
If (expression)
Statement;
If($a>$b) {
Echo a is larger than b; }
Note: ifstatement.php
Array
Note: arrays.php
While Loop:
While (expression)
Statement;
While ($count<=10) {
Echo $count; $count++; Note: while.php }
Note: for.php
Foreach Loop:
Foreach ($array as $value) Statement;
Note: foreach.php
Note: continue.php
Note: function.php
Function arguments
Predefined Function
Note: prefunction.php
Note: boolean.php
Constants
Note: constant.php
Overview of phpMyAdmin
Note: mysql1.php
Insert
Update
Delete
Create Form
Note: form.html
PEAR
PEAR is a framework and distribution system for reusable PHP components. What is PEAR? PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide: A structured library of open-source code for PHP users A system for code distribution and package maintenance A standard style for code written in PHP The PHP Extension Community Library (PECL), A web site, mailing lists and download mirrors to support the PHP/PEAR community PEAR is a community-driven project governed by its developers. PEAR's governing bodies are subdivided into the PEAR Group, Collectives, and a President. PEAR's constitution (adopted in March 2007) defining these groups is documented Many components/ packages for PHP can be downloaded from http://pear.php.net/, installed and then the functionality can be used. http://pear.php.net/packages.php PEARDb DB abstraction layer http://pear.php.net/package/DB/ Useful for cases where we might foresee a change in DB backend This package has been superseded, Use MDB2 instead. http://pear.php.net/package/MDB2
Stuti Kumar
Include function
Explanation: The function include() allows embedding one file inside another. Difference between require and include
COOKIES
Explanation: The third form of deriving information from the user is cookies. The syntax used to write is : Setcookies($name,$value,$expire); <?php setcookie(testNIC, id45, time()+(60*60*24*7)); ?> Code explanation: Name testNIC, ID is 45, current time time() + 60 sec * 60 min * 24 hours * 7 days
Unset a Cookie
<?php setcookie(testNIC, id45, time()-(60*60*24*7)); ?>
Session
A cookie stores variables on the visitor's machine whereas a session stores the variables on the server and just gives the visitor a cookie with an ID corresponding to those variables. Advantage to use session:
Creates a file on the webserver Can store lot of information Long code & complicated for user to hack hence more secure.
User form
Note: user_add.php Note: user_edit.php Hidden variable
File Functions
fopen 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. 'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it.
File Permissions
Chmod change permissions of files. <?php // Read and write for owner, nothing for everybody else chmod("/somedir/somefile", 0600); // Read and write for owner, read for everybody else chmod("/somedir/somefile", 0644); // Everything for owner, read and execute for others chmod("/somedir/somefile", 0755); // Everything for owner, read and execute for owner's group chmod("/somedir/somefile", 0750); ?>
File Ownership
Chown change owner of files. <?php // change owner to user suchi chown("/somedir/somefile", suchi); ?>
File Locking
flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows)