UNIT - I
Introduction to PHP: Declaring variables, data types, arrays, strings, operators,
expressions, control structures, functions, Reading data from web form controls like
text boxes, radio buttons, lists etc., Handling File Uploads, Connecting to database
(MySQL as reference),executing simple queries, handling results, Handling sessions
and cookies.
File Handling in PHP: File operations like opening, closing, reading, writing,
appending, deleting etc. on text and binary files, listing directories
Introduction
PHP (Hypertext Preprocessor) is an open source, interpreted and object-oriented
scripting language i.e. executed at server side. It is used to develop web applications
(an application i.e. executed at server side and generates dynamic page). It was
developed by Rasmus Lerdorf in 1994.
PHP Features:
Performance: Script written in PHP executes much faster then those scripts written
in other languages such as JSP & ASP.
Open Source Software: PHP source code is free available on the web, you can
developed all the version of PHP according to your requirement without paying any
cost.
Platform Independent: PHP are available for WINDOWS, MAC, LINUX & UNIX
operating system. A PHP application developed in one OS can be easily executed in
other OS also.
Compatibility: PHP is compatible with almost all local servers used today like
Apache, IIS etc.
Embedded: PHP code can be easily embedded within HTML tags and script.
Install PHP
To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software
stack. It is available for all operating systems. There are many AMP options available
in the market that are given below:
WAMP for Windows
LAMP for Linux
MAMP for Mac
SAMP for Solaris
FAMP for FreeBSD
XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some
other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail etc.
,If you are on Windows and don't want Perl and other features of XAMPP, you should
go for WAMP. In a similar way, you may use LAMP for Linux and MAMP for
Macintosh.
PHP Variables
A variable in PHP is a name of memory location that holds data. A variable is a
temporary storage that is used to store data temporarily.
In PHP, a variable is declared using $ sign followed by variable name. Syntax of
declaring a variable in PHP is given below:
$variablename=value;
Rules for PHP variables:
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must starts with a letter or the underscore symbol
• Variable names consist of letters ,digits and underscore symbol (A-z, 0-9, and
_)
• White spaces are not allowed
• These can be of any length.
• Variable names are case-sensitive ($vaag and $VAAG are two different
variables)
Ex: $abc $_xyz $lmn12
PHP Echo
PHP echo is a language construct not a function, so you don't need to use
parenthesis with it. But if you want to use more than one parameters, it is required
to use parenthesis.
The syntax of PHP echo is given below:
void echo ( string $arg1 [, string $... ] )
PHP echo statement can be used to print string, multi line strings, escaping
characters, variable, array etc.
PHP echo: printing string
File: echo1.php
<?php
echo "Hello by PHP echo";
?>
Output: Hello by PHP echo
,PHP echo: printing multi line string
File: echo2.php
<?php
echo "Hello by PHP echo
this is multi line
text printed by
PHP echo statement ";
?>
Output:
Hello by PHP echo this is multi line text printed by PHP echo statement
PHP echo: printing escaping characters
File: echo3.php
<?php
echo "Hello escape \"sequence\" characters";
?>
Output:
Hello escape "sequence" characters
PHP echo: printing variable value
PHP Operators
An Operator is a symbol that specifies an operation to be performed on the
operands.
Types of operators.
1. Arithmetic operators.
2. Assignment Operators
3. Incement and decrement Operators
4. Comparison operators
5. Conditional operators
6. Logical Operators.
7. Bitwise Operators.
, 8. String Operators.
1. Arithmetic operators.
These are used to perform basic arithmetic operations
There are five basic arithmetic operators.
+ (addition), - (subtraction), * (multiplication), / (division), % (modulus)
The operators are summarized in the following table.
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y.
- Subtraction $x - $y Difference of $x and $y.
* Multiplication $x * $y Product of $x and $y.
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y.
Example:
<?php
$x=100;
$y=60;
echo "The sum of x and y is : ". ($x+$y) ."<br />";
echo "The difference between x and y is : ". ($x-$y) ."<br />";
echo "Multiplication of x and y : ". ($x*$y) ."<br />";
echo "Division of x and y : ". ($x/$y) ."<br />";
echo "Modulus of x and y : " . ($x%$y) ."<br />";
?>
Output:
The sum of x and y is : 160
The difference between x and y is : 40
Multiplication of x and y : 6000
Division of x and y : 1.6666666666667
Introduction to PHP: Declaring variables, data types, arrays, strings, operators,
expressions, control structures, functions, Reading data from web form controls like
text boxes, radio buttons, lists etc., Handling File Uploads, Connecting to database
(MySQL as reference),executing simple queries, handling results, Handling sessions
and cookies.
File Handling in PHP: File operations like opening, closing, reading, writing,
appending, deleting etc. on text and binary files, listing directories
Introduction
PHP (Hypertext Preprocessor) is an open source, interpreted and object-oriented
scripting language i.e. executed at server side. It is used to develop web applications
(an application i.e. executed at server side and generates dynamic page). It was
developed by Rasmus Lerdorf in 1994.
PHP Features:
Performance: Script written in PHP executes much faster then those scripts written
in other languages such as JSP & ASP.
Open Source Software: PHP source code is free available on the web, you can
developed all the version of PHP according to your requirement without paying any
cost.
Platform Independent: PHP are available for WINDOWS, MAC, LINUX & UNIX
operating system. A PHP application developed in one OS can be easily executed in
other OS also.
Compatibility: PHP is compatible with almost all local servers used today like
Apache, IIS etc.
Embedded: PHP code can be easily embedded within HTML tags and script.
Install PHP
To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software
stack. It is available for all operating systems. There are many AMP options available
in the market that are given below:
WAMP for Windows
LAMP for Linux
MAMP for Mac
SAMP for Solaris
FAMP for FreeBSD
XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some
other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail etc.
,If you are on Windows and don't want Perl and other features of XAMPP, you should
go for WAMP. In a similar way, you may use LAMP for Linux and MAMP for
Macintosh.
PHP Variables
A variable in PHP is a name of memory location that holds data. A variable is a
temporary storage that is used to store data temporarily.
In PHP, a variable is declared using $ sign followed by variable name. Syntax of
declaring a variable in PHP is given below:
$variablename=value;
Rules for PHP variables:
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must starts with a letter or the underscore symbol
• Variable names consist of letters ,digits and underscore symbol (A-z, 0-9, and
_)
• White spaces are not allowed
• These can be of any length.
• Variable names are case-sensitive ($vaag and $VAAG are two different
variables)
Ex: $abc $_xyz $lmn12
PHP Echo
PHP echo is a language construct not a function, so you don't need to use
parenthesis with it. But if you want to use more than one parameters, it is required
to use parenthesis.
The syntax of PHP echo is given below:
void echo ( string $arg1 [, string $... ] )
PHP echo statement can be used to print string, multi line strings, escaping
characters, variable, array etc.
PHP echo: printing string
File: echo1.php
<?php
echo "Hello by PHP echo";
?>
Output: Hello by PHP echo
,PHP echo: printing multi line string
File: echo2.php
<?php
echo "Hello by PHP echo
this is multi line
text printed by
PHP echo statement ";
?>
Output:
Hello by PHP echo this is multi line text printed by PHP echo statement
PHP echo: printing escaping characters
File: echo3.php
<?php
echo "Hello escape \"sequence\" characters";
?>
Output:
Hello escape "sequence" characters
PHP echo: printing variable value
PHP Operators
An Operator is a symbol that specifies an operation to be performed on the
operands.
Types of operators.
1. Arithmetic operators.
2. Assignment Operators
3. Incement and decrement Operators
4. Comparison operators
5. Conditional operators
6. Logical Operators.
7. Bitwise Operators.
, 8. String Operators.
1. Arithmetic operators.
These are used to perform basic arithmetic operations
There are five basic arithmetic operators.
+ (addition), - (subtraction), * (multiplication), / (division), % (modulus)
The operators are summarized in the following table.
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y.
- Subtraction $x - $y Difference of $x and $y.
* Multiplication $x * $y Product of $x and $y.
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y.
Example:
<?php
$x=100;
$y=60;
echo "The sum of x and y is : ". ($x+$y) ."<br />";
echo "The difference between x and y is : ". ($x-$y) ."<br />";
echo "Multiplication of x and y : ". ($x*$y) ."<br />";
echo "Division of x and y : ". ($x/$y) ."<br />";
echo "Modulus of x and y : " . ($x%$y) ."<br />";
?>
Output:
The sum of x and y is : 160
The difference between x and y is : 40
Multiplication of x and y : 6000
Division of x and y : 1.6666666666667