Pixels Infotech
PHP Material
1st Day
PHP - Hypertext Preprocessor (or) Personal Home Page.
PHP is a Scripting Language.
PHP Scripting Language is of 3 types.
a) Command Line Scripting.
b) Desktop/GUI Based Applications - Graphical User Interface.
c) Web Applications.
The main purpose of PHP language is to develop web applications.
A web application is a request, response mechanism (or) a client server methodology.
The programs present in the client machine to send a request to the server, to send data to the
server and also to get the response from server is known US client side program (Browser).
The programs present in the server machine to receive the request from the client, to process
the request and to give and appropriate response back to the client is known as server side
applications.
PHP Language is used for developing server side applications.
Tools - Internet Options - Language.
HTTP Request + HTTP Response.
When a client is sending a request HTTP protocol will create an HTTP request object which
contains all the HTTP details of the client and that object will be sent to the server, their it get
process an HTTP response object gets created and that object will come to the client side.
PHP Scripts are being encoded in the server side like ASP.
Another future of PHP is PHP can work with most of the database My SQL, Oracle, SQL Server
etc.
PHP is an open source product.
PHP can be downloaded free of cost.
PHP applications are platform independent that means if you develop a PHP application in one
operating system. That application you can run in any other operating system.
PHP applications can be deployed in most of the web servers like IIS, APACHE, Personal Web
Server, Samba Server etc.
A web server is an application running in the server machine capable of receiving HTTP request
every web server is identified with a specific port.
Port Number means Server Identification Number.
2nd Day
History of PHP:-
PHP succeeds an older product, named PHP/FI.
PHP/FI was created by Rasmus Lerdorf in 1995.
PHP/FI was introduced in November 1997 using C language.
PHP/FI stands for Personal Home Page / Forms Interpreter.
PHP/FI was introduced from PERL scripting language.
PHP 3.0 was introduced in 1998 by Andi Gutmans and Zeev Suraski.
PHP 4.0 was introduced in 2000.
PHP 5.0 was released in 2004.
A PHP program gets encoded in the server and will give the response back to the client in pure
HTML.
Flow of PHP Program
Zend Engine can also be called as parser.
The Zend Engine converts a PHP program to HTML.
PHP Scripts are used for developing
Server Side Applications – PHP parser, a web server and a browser in the client side
is required
Command Line Scripting – only PHP parser is required.
Desktop or GUI Applications – PHP – GTK is used for developing GUI applications.
GTK stands for GIMP Tool Kit.
GIMP stands for GNU Image Manipulation Program.
3rd Day
Install IIS:-
IIS is Internet Information Services.
It is the default browser in windows.
IIS is windows component.
Start - Settings - Control Panel - Add or Remove Programs
Add or Remove Programs - windows components.
Select IIS from list.
Click on next button insert windows XP cd.
Every web server has got a server directory where we need to place our server side programs
In the case of IIS the default server directory is C: /inetmgr/wwwroot
The default port of IIS is 80.
To check weather the server is running or not we can use a URL from the client side.
URL stands for Uniform Resource Locator.
A URL contains 4 ports
Protocol
Hostname
Port No
Resource Name in that Protocol and Hostname are mandatory.
http://localhost
http://name (pixels)
http://IP address
We to test our program are running properly or not.
<html>
<body bg color= pink>
<h1> Response from IIS web server</h1>
</body>
</html>
Save our program us pixels.html in C:\inetpub\wwwroot folder
Client invokes pixels.html by the using URL http://localhost/pixels.html
For PHP
<? PHP
echo “<h1> Response from a PHP program in IIS</h1>
?>
Save the above program as pixels.php in C:/inetpub/wwwroot
Client can invoke pixels.php by the using URL http://localhost/pixels.php
To executing any server side program the corresponding handler is required in the web server.
Download and configure IIS with PHP.
Download PHP from www.php.net/downloads.php
PHP-5.2.14-win32.zip July 22 Last Month
Extract the above file to any directory (C:\PHP)
PHP 5 isapi.dll is the file or the handler executable
ISAPI stands for internet services application programming interface
Open IIS control panel
Start - Run - Inetmgr
Expand Local Computer
Expand websites - Properties (Default Website Properties dialog box will be displayed).
Select “Home directory” tab from the properties dialog box click of configure button.
Click on add button
4th Day
To develop a real time PHP project we required the following products.
PHP
Data Base Server - My SQL
Web Server - APACHE
FTP Client - To Upload/Download Files
Mail Server
Remote DB Administration Tool
By a single installation all the above products should be installed in our system, for that we can
use any of the following installers.
WAMP - Windows, Apache, My SQL, PHP
LAMP - Linux, Apache, My SQL, PHP
MAMP - Mamptost, Apache, My SQL, PHP
XAMPP - Cross Platform, Apache, My SQL, PHP Interpreter, PERL Interpreter
Download and Install XAMPP:-
Download XAMPP for windows from internet (Google it)
XAMPP-win32-1.7.3.exe installs it.
XAMPP Installs:-
PHP
PERL
APACHE Web Server
My SQL DB Server
Mercury - Mail Server
File Zilla - FTP Client
PHP My Admin - Remote DB Administration Tool etc.
Apache web server is working with the default port of 80.
In a case of XAMPP the default server directory is C: /XAMPP/htdocs that means all the server
side programs has to be placed in htdocs folder.
Place pixels.php and pixels.html in htdocs folder
Start APACHE Web Server:-
Open XAMPP Control Panel
Click on start button of Apache
Note: - click on “Port - check” button in XAMPP Control Panel to check the available ports.
Open browser and provide the URL as http://localhost
How to change the port of web server from 80 to 8082?
Open “htpd.conf” files from C:\XAMPP\Apache\conf folder
Search for “Listen 80” and replace 80 with 8082.
Search for “Server name localhost: 80” and replace 80 with 8082.
Save the file and restart Apache Web Server; now apache is working in the port 8082.
The URL to be used is http://localhost:8082 to invoke pixels.php program we can use the
URL http://localhost:8082/pixels.php
5th Day
PHP Project contains different types of files like.
HTML
CSS (Case Cading Style Sheet)
JS (Java Script)
PHP Files
PHP Interfaces
PHP Classes
XML Files
Sub Directories
Images, Sound Files, Video Files etc.
The different IDES – (Integrated Development Environments) used for developing a PHP.
PHP Designer
Rapid PHP
Zend Studio
PHP Edit
PHP ED
Dream Weaver
Code Lobster
Net Beans IDE for PHP, etc.
Download and Install Net Beans IDE for PHP:-
Download the IDE from www.netbeans.org/downloads
Netbeans-6.8-ml-php-windows.exe installs the above file.
Create a PHP Project:-
Open Net Beans IDE for PHP
File Menu - New Project - Select PHP from categories and select “PHP Application” from
Project - Next - Project Name: PHP Proj24 - Next - Run As : local website
Project URL: http://localhost:8082/PHP Proj24/ - Finish.
As a result of the above steps a new directory gets created with the name of project
(PHP Proj24) in C:\XAMPP\htdocs folder with index.php as the default home page.
To integrate a new PHP program to the project.
Right click on the project Node - New - PHP File
File Name: One - Finish
<? PHP
echo “<h1> Response from one.php”
?>
One.php program presenting PHP Proj24 can be invoked by the client.
Open Browser
Provide the URL as http://localhost:8082/phpproj24/one.php
To check the program:
Select one.php - Right Click - Run
Select one.php press - shift F6, Press just F5
Basic PHP Program:-
A PHP file contains HTML Tags, Plain Text, and PHP Scripts
A PHP program should be saved with the extension of - PHP
In a PHP program server side PHP Scripts are integrated in side a mark up language – HTML
In a PHP program server side PHP Scripts should be written inside a PHP Block.
A PHP Block can be present any where in the program and any number of times
A standard PHP Block always starts with <? PHP and ends with ?>
A standard Scripting Block is understandable for any web server that supports PHP.
On servers with shorthand support a enabled you can start a scripting block with <? End with?>
And also ASP Style block which starts with <% and ends with %>
Shorthand open block and ASP Style block will be allowed by a web server only if it is configured
in php.ini
Php.ini is present in C:\XAMPP\PHP Folder.
Search for the value of “short_open_tag” and “asp_tags” in php.ini file to identify whether the
web server supports short open block or asp block.
echo and Print are the language constructs of PHP used for out putting text or to give a response
back to the client
Semi Colon is used to separate multiply PHP Scripts
Comments in PHP:-
Single line comments - // or #
Block comments starts with /* and ends with */
6th Day
<html>
<body bgcolor=”cyan”>
<h1 align = center> Pixels Infotech::: PHP Proj24 </h1>
<? PHP
echo “<h2> Response from PHP Block </h2>
print “**** </h2>”
?>
<hr color =”red” size=”10”>
<? PHP
echo “<h2> Response from PHP Block – 2”
?>
</body>
</html>
Variables in PHP:-
Variables are used for strong values like text strings, numbers or arrays.
When a variable is set it can be used over and over again in your script.
All variables in PHP starts with a “$” (Dollar Sign Symbol).
In PHP a variable does not need to be declared before being set.
You do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on how they are set.
In PHP the variable is declared automatically when you use it.
Variable Naming Rules:-
A PHP variable name can have alpha numeric characters and also _ (Under Score).
A PHP variable name should start with a letter or _ (Under Score).
No spaces allow between the characters of a variable name.
It is advisable note to use reserved keywords of PHP as a variable name.
Dot (Pried Operator) is acting the can operator used for appending different types of data
together.
Example 1:-
<? php
echo "<h1>";
$Firstname ="Pixels";
$Lastname ="Infotech";
$_age ="3 years";
echo $Firstname."----".$Lastname."----". $_age."<br>";
echo "$Firstname----$Lastname----$_age<br>";
echo '$Firstname----$Lastname----$_age<br>';
?>
Note: - if you echo a variable in Double Quotes (“”) self expansion of the variable will happen
where the memory usage is small.
PHP Data Types:-
PHP Supports 8 types of Data.
4 Scalar Types (a variable in which only one value can be stored at a time is known as scalar
type).
Boolean
Integer
Float / Double
String
2 Compound Types (a variable in which multiple values can be stored).
Array
Object
2 Special Types
Resource
Null
The type of a variable is usually not set by the program; rather, it is decided at runtime by PHP
depending on the context in which that variable is used.
var_dump (variable_name) is a built in function of PHP used for getting the type of the variable
with content.
Example 2:-
<?php
echo "<h1>";
$x =10;
var_dump($x);
echo "<br>";
echo "<h1>";
$x =10.65;
var_dump($x);
echo "<br>";
echo "<h1>";
$x =Pixels;
var_dump($x);
echo "<br>";
echo "<h1>";
$x =False;
var_dump($x);
echo "<br>";
?>
7th Day
gettype (variable_name) is a built in function PHP used for getting the type of a variable.
settype (variable_name, new type) is a built in function of PHP used for setting a new type to a
variable.
unset (variable_name) is a built in function of PHP used for removing the content of a variable.
Example 3:-
<?PHP
Echo “<h1>”;
$x = ”10A”;
var_dump ($x);
echo “<br>”;
echo gettype ($x).”----”.$x. “<br>”;
settype ($x, “integer”);
var_dump ($x);
echo “<br>”;
echo gettype ($x). “----“ .$x. “<br>”;
unset ($x);
var_dump ($x);
echo “<br>”;
echo gettype ($x). “----“ .$x. “<br>”;
?>
<?PHP
$a = “Pixels”;
$b = “Infotech”;
$c = $a+$b;
Echo “Result =”.$c;
?>
To check whether a variable is a particular type we can use if statement as follows.
<?PHP
$a = ?
If (gettype ($a) == “integer”)
echo “variable is of INTEGER type”;
else
echo “variable is not of INTEGER type”;
?>
is _ [type] (variable_name)
set of function including is_into (), is_double (), is_float (), is_bool (), is_string (), is_array (),
is_integer (), is_object (), etc. to check whether a specific variable matches the data type.
is_ [type] (----)
<?PHP
$a = “10”;
If (is_int ($a))
Echo “variable is of Integer type”;
?>
Constant Variables in PHP:-
A variable whose value can not be change at any point of time or a variable that can not be
assigned is known as a constant variable.
In PHP a constant variable should not start with $ (Dollar Sign) symbol.
A constant variable is defined in PHP by using the function define()
Define (constant variable_name, value)
PHP_OS is a pre defined built in constant of PHP used for getting the name of the operating
system in which current PHP installation is done.
PHP info ( ) is a built in function of PHP used for getting the complete configuration details about
PHP.
<?php
Define (“Name, “Pixels Infotech”);
Define (“AGE” , 13);
Define (“Name”, “Accenture”);
Echo Name. “----” . AGE, “<br>”;
Echo PHP_OS. “<br>”;
phpinfo ();
?>
The costing is a process of converting one type of data to another type.
<? php
echo “<h1>”;
$var1 = “12.65”;
$new int = (int) $var1;
$new float = (float) $var1;
echo $ new int. “----” $ new float
?>
With respective data types variables are classified into two types.
Value Type:-
int a = 10
int b = a
b = 20;
Reference Type:-
Employee emp 1 = new employee ( )
Employee emp 2 = emp1;
Reference means memory address
Class Description
Class Overview
In this PHP training course, students will learn to create database-driven websites using PHP and
MySQL or the database of their choice.
Class Goals
Learn how PHP works
Learn the basic syntax of PHP
Learn to create dynamic interactive pages with PHP
Learn to manipulate files with PHP
Learn to work with arrays in PHP
Learn to validate forms with PHP
Learn to write functions in PHP
Learn to manipulate and manage database data with PHP
Learn to authenticate users with PHP
Learn to manage sessions with PHP
Learn to work with the PEAR:DB module
Learn advanced form validation with regular expressions
Learn to send email with PHP
Class Outline
1. PHP Basics
1. How PHP Works
2. The php.ini File
3. Basic PHP Syntax
1. PHP Tags
2. PHP Statements and Whitespace
3. Comments
4. PHP Functions
5. Hello World!
4. Variables
1. Variable Types
2. Variable Names (Identifiers
3. Type Strength
4. Hello Variables!
5. Variable Scope
6. Superglobals
7. Constants
8. Variable-Testing and Manipulation Functions
5. First PHP Script
6. PHP Operators
7. Creating Dynamic Pages
1. Single Quotes vs. Double Quotes
2. Passing Variables on the URL
8. Passing Variables via the Query String
2. Flow Control
1. Conditional Processing
1. If Conditions
2. Working with Conditions
3. Loops
1. while
2. do...while
3. for
4. break and continue
4. Working with Loops
3. Arrays
1. Enumerated Arrays
1. Initializing Arrays
2. Appending to an Array
3. Reading from Arrays
4. Looping through Arrays
2. Working with Enumerated Arrays
3. Associative Arrays
1. Initializing Associative Arrays
2. Reading from Associative Arrays
3. Looping through Associative Arrays
4. Superglobal Arrays
4. Working with Associative Arrays
5. Two-dimensional Arrays
1. Reading from Two-dimensional Arrays
2. Looping through Two-dimensional Arrays
6. Array Manipulation Functions
4. PHP and HTML Forms
1. HTML Forms
1. How HTML Forms Work
2. A Sample HTML Form
3. Form Variables
2. Processing Form Input
5. String Manipulation
1. Formatting Strings
1. Concatenation
2. String Manipulation Functions
3. Examples of String Functions
2. Working with String Manipulation Functions
3. Magic Quotes
1. magic_quotes_gpc
2. magic_quotes_runtime
3. Recommendation on Magic Quotes
6. Reusing Code and Writing Functions
1. Including Files
1. require
2. require_once
3. auto_prepend_file and auto_append_file
2. Adding a Header and Footer
3. User Functions
1. Defining and Calling Functions
2. Default Values
3. Variable Scope
4. By Reference vs. By Value
4. Form Processing
1. Code Organization
5. Form Validation and Presentation Functions
7. Managing Data
1. Querying a Database
1. mysqli() Overview
2. mysqli Methods and Properties
3. Inserting and Updating Records
4. mysqli Prepared Statements
2. Inserting Records
8. PEAR:DB
1. Advantages and Disadvantages of PEAR DB
1. Why use a database abstraction layer?
2. When not to use a database abstraction layer?
2. Using PEAR DB
3. Creating a Customer Report
9. Authentication with PHP and SQL
1. A Database-less Login Form
2. Authenticating Users
10. Regular Expressions
1. Perl-compatible Regular Expression Functions
1. preg_match()
2. preg_replace()
3. Regular Expression Tester
2. Regular Expression Syntax
1. Start and End ( ^ $ )
2. Number of Occurrences ( ? + * {} )
3. Common Characters ( . \d \D \w \W \s \S )
4. Grouping ( [] )
5. Negation ( ^ )
6. Subpatterns ( () )
7. Alternatives ( | )
8. Escape Character ( \ )
3. Form Validation Functions with Regular Expressions
11. Session Control and Cookies
1. Sessions
1. Configuring Sessions
2. Session Functions
2. Cookies
3. Authentication with Session Control
12. Sending Email with PHP
1. mail()
1. Shortcomings of mail()
2. PHPMailer
3. Sending a Password by Email
13. File System Management
1. Opening a File
1. fopen()
2. Reading from a File
1. fgets()
3. Writing to a File
1. fwrite()
4. Writing to a File
5. File Locking
1. flock()
6. Uploading Files via an HTML Form
7. Getting File Information
8. More File Functions
9. Directory Functions
1. Getting a Directory Listing
10. Creating a Resume Management Page