Unit 1
Unit 1
CHAPTER I
INTRODUCTION
What is PHP?
PHP is an acronym for "PHP Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP costs nothing, it is free to download and use
What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code are executed on the server, and the result is returned to the browser as plain HTML
PHP files have extension ".php"
What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can restrict users to access some pages on your website
PHP can encrypt data
With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies.
You can also output any text, such as XHTML and XML.
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free.
PHP is easy to learn and runs efficiently on the server side
Characteristics of PHP
1. Performance
Scripts written in PHP execute faster than those written in other scripting languages, with many
independent standards putting the language ahead of competing alternatives like JSP, ASP.NET,
and Perl.
protocol://hostname/other_information
When developing a PHP application for the Web, the typical approach is to embed PHP code into one or
more standard HTML documents using special “tags,” or delimiters. Here’s an example:
<html>
<head></head>
<body>
<div>
<?php echo sqrt(49); ?>
</div>
</body>
</html>
From the preceding explanation, it should be clear that to get started building PHP applications,
your development environment must contain at least three components:
A base operating system (OS) and server environment (usually Linux)
A Web server (usually Apache on Linux or IIS on Windows) to intercept HTTP requests and either
serve them directly or pass them on to the PHP interpreter for execution
<?php
<?php
?>
The error message generated by the parser is quite helpful: it tells you what the error was, as
This makes it fairly easy—in most cases—to locate and correct the error.
When the PHP parser reads a script, it executes only the code found between PHP tags; everything
outside these tags is ignored by the parser and returned “as is.”
This makes it extremely easy to embed PHP code within an HTML document to create Web pages
that have all the formatting bells and whistles of standard HTML but can additionally perform
complex calculations or read and manipulate data from external sources (such as databases or Web
services).
Department of Computer Science Page 8
EXAMPLE:
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>HTML Color Table</title>
<style type="text/css">
body{
font-family: Verdana sans-serif;
}
td {
border: solid 5px white;
}
</style>
</head>
<body>
<h2>Colors with HTML and PHP</h2>
<table>
<tr>
<td>Blue</td>
<td style="width:40px; background-color:#0000ff"></td>
</tr>
<tr>
td><?php echo 'Red'; ?></td>
<td style="width:40px;
background-color:<?php echo '#ff0000'; ?>"></td>
</tr>
<?php
// this row generated through PHP
echo "<tr>\n";
echo " <td>Green</td>\n";
echo " <td style=\"width:40px; background-color:#00ff00\"></td>\n";
echo "</tr>\n";
?>
</table>
A Web page containing colors and color codes, generated by mixing PHP with HTML
When the parser encounters one of these escape sequences, it knows to replace it with the
corresponding value before sending it to the output device. Consider, for example, this line of
code:
<?php
echo "You said \"Hello\"";
?>
CHAPTER II
In this example, the variable $name is assigned the value 'Simon'. The echo statement is then
used to print the value of this variable to the Web page.
2. DESTROYING VARIABLES
To destroy a variable, pass the variable to PHP’s aptly named unset() function, as in the
following example:
<?php
// assign value to variable
$car = 'Porsche';
// print variable value
// output: 'Before unset(), my car is a Porsche'
echo "Before unset(), my car is a $car";
// destroy variable unset($car);
// print variable value
// this will generate an 'undefined variable' error
// output: 'After unset(), my car is a '
echo "After unset(), my car is a $car";
?>
3. Boolean:
They have only two possible values either true or false.
PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE, which
can be used like so:
EXAMPLE:
if (TRUE)
print("This will always print<br>");
else
print("This will never print<br>");
4. NULL:
NULL is a special type that only has one value: NULL.
To give a variable the NULL value, simply assign it like this:
EXAMPLE:
$my_var = NULL
5. Strings:
They are sequences of characters, like "PHP supports string operations".
Following are valid examples of string
EXAMPLE:
$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, singly quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters
?>
This example introduces PHP’s gettype() operator, which is a handy little tool for finding out
the type of a particular variable.
As the script output demonstrates, the variable $whoami begins life as a string, assigned the value
'Sarah'.
It’s then assigned the number 99.8, which automatically converts it to a floating-point variable.
Following this, the variable is de-initialized with the unset() method, which removes its value
and turns it into a NULL.
PHP has been the invisible hand behind each of these conversions, internally resetting the data
type of $whoami from string to floating-point to null.
Casting is a technique commonly used by Java programmers; to use it, simply specify the desired
data type in parentheses on the right side of the assignment equation.
Consider the following example, which illustrates turning a floating-point value into an integer
value:
EXAMPLE:
<?php
$speed = 501.789; // define floating-point variable
In addition to the gettype() function, PHP includes a number of more specialized functions, to
test if a variable is of a specific type.
USING CONSTANTS
Constants are PHP containers for values that remain constant and never change.
They’re mostly used for data that is known well in advance and that is used, unchanged, in multiple
places within your application.
Constants are defined using PHP’s define() function, which accepts two arguments: the name
of the constant, and its value.
Constant names must follow the same rules as variable names, with one exception: the $ prefix is
not required for constant names.
Here’s an example of defining and using a constant in a script:
EXAMPLE:
<?php
define ('PROGRAM', 'The Matrix'); // define constants
define ('VERSION', 11.7); // use constants
// output: 'Welcome to The Matrix (version 11.7)'
echo 'Welcome to ‘ . PROGRAM . (version . VERSION . ')';
?>
MANIPULATING VARIABLES WITH OPERATORS
Variables are simply containers for information.
In order to do anything useful with them, you need operators.
Operators are symbols that tell the PHP processor to perform certain actions.
For example, the addition (+) symbol is an operator that tells PHP to add two variables or values,
while the greater-than (>) symbol is an operator that tells PHP to compare two values.
PHP supports more than 50 such operators, ranging from operators for arithmetical operations to
operators for logical comparison and bitwise calculations.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide and return quotient
% Divide and return modulus
Common Arithmetic Operators
EXAMPLE:
<?php
$x = 10; // define variables
$y = 5;
$z = 3;
$sum = $x + $y; // add
echo "$x + $y = $sum\n";
$diff = $x - $y; // subtract
echo "$x - $y = $diff\n";
$product = $x * $y; // multiply
echo "$x * $y = $product\n";
$quotient = $x / $y; // divide and get quotient
echo "$x / $y = $quotient\n";
$modulus = $x % $y; // divide and get modulus
echo "$x % $y = $modulus\n";
?>
Concatenating Strings
To combine strings, use PHP’s concatenation operator, which happens to be a period (.). The
following example illustrates:
EXAMPLE:
<?php
$country = 'England'; // define variables
$city = 'London';
// combine into single string
// output: 'Welcome to London, the coolest city in all of England'
echo 'Welcome to ' . $city . ', the coolest city in all of ' .
// test if $q is equal to $r
// returns false
echo ($q == $r);
?>
Operator Description
Department of Computer Science Page 18
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
=== Equal to and of the same type
Common Comparison Operators
Per forming Logical Tests
When building complex conditional expressions, PHP’s three most commonly used logical
operators, listed in
Operator Description
&& AND
|| OR
! NOT
EXAMPLE:
<?php
// define variables
$price = 100;
$size = 18;
// logical AND test
// returns true if both comparisons are true
// returns true here
echo ($price > 50 && $size < 25);
// logical OR test
// returns true if any of the comparisons are true
// returns false here
echo ($price > 150 || $size > 75);
// logical NOT test
// reverses the logical test
// returns false here
echo !($size > 10);
?>
Other Useful Operators
There are a few other operators that tend to come in handy during PHP development.
First, the addition-assignment operator, represented by the symbol +=, lets you simultaneously add
Operator Description
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign quotient
%= Divide and assign modulus
.= Concatenate and assign (strings only)