UNIT-IV
SERVER-SIDE PROCESSING AND SCRIPTING-PHP
PHP is an open source programming and server scrip ng language that is par cularly well adapted to
Create Dynamic web pages and can be includes in HTML codes. It’s used to handle complex content,
databases, and session logging, as well as to create en re e-commerce websites. PHP is generally known
as Hypertext preprocessor but it was first known as Personal Home Page. It was Designed in 1994 by the
programmer named Rasmus Lerdorf. PHP would be rela vely simple .PHP is compa ble with a variety of
databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microso SQL Server, and can
communicate with other services through protocols including LDAP, POP3, HTTP, IMAP, and COM.
And, PHP operates on a variety of pla orms, including Windows, Linux, Unix, and Mac OS X,etc.
Example of PHP Code!
<!DOCTYPE html>
<html>
<body> <?php echo "My
first PHP script!";
?>
</body>
</html>
PHP Works:
The PHP program interacts with the site server, which is the program that sends out web pages to the rest
of the world. When you type a URL into the address bar of your web browser, you’re telling the web server
at that URL to email you an HTML file. The requested file is sent by the web server in response. The HTML
file is read by your browser, and then shows the web page. When you press a source on a web page, you’re
also reques ng a file from the web server. Addi onally, when you press a web page bu on that submits a
form, the web server processes a file. When PHP is mounted, the procedure is exactly the same.
You submit a file, and the web server, which happens to be running PHP, responds with HTML, because of
PHP it all happens.
Step 1 – Client send a page request to the web server.
Step 2 – Web server forwards that request to the PHP interpreter.
Step 3 – Now PHP interpreter will take the Date from Database and responce it back to the Web server.
Step 4 – At last Web server responce to the client who has asked for the page request.
2. Explain PHP variables and constants?
Variables are "containers" for storing informa on.
PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable
Example:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
A er the execu on of the statements above, the variable $txt will hold the value Hello world!, the
variable $x will hold the value 5, and the variable $y will hold the value 10.5.
PHP Variables
A variable can have a short name (like x and y) or a more descrip ve name (age, carname, total_volume).
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name must start wi th a le er or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha -numeric characters and underscores (A -z, 0-9, and _ )
Variable names are case -sensi ve ($age and $AGE are two different variables)
PHP variable names are case-sensi ve!
The PHP echo statement is o en used to output data to the screen.
The following example will show how to output text and a variable:
•
Output variables:
Example:
<?php
$txt = "Msajce"; echo
"I love $txt!"; ?>
PHP Variables :
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
name: Specifies the name of the constant
PHP
has value: Specifies the value of the constant
three
case-insensi ve: Specifies whether the constant name should be case -insensi ve. Default is false
different variable :
• local
• global
• sta c
PHP Constants
A constant is an iden fier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a le er or underscore (no $ sign before the constant name).
Unlike variables, constants are automa cally global across the en re script.
Constants are like variables except that once they are defined they cannot be changed or undefined.
Create a PHP Constant:
To create a constant, use the define() func on.
Syntax define(name, value, case-insensi ve)
Parameters:
Example:
<?php
// case-sensi ve constant name
define("GREETING", "Welcome to msajce!"); echo
GREETING;
?> o/p:
Welcome to msajce!
3. Explain PHP operators with example?
PHP Operators:
Operators are used to perform opera ons on variables and values.
PHP divides the operators in the following groups:
• Arithme c operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Condi onal assignment operators
PHP Arithme c Operators
The PHP arithme c operators are used with numeric values to perform common arithmet ical opera ons,
such as addi on, subtrac on, mul plica on etc.
PHP Assignment Operators:
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the le operand gets set to the value of the
assignment expression on the right.
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
PHP Logical Operators
The PHP logical operators are used to combine condi onal statements.
PHP String Operators
PHP has two operators that are s pecially designed for strings.
PHP Array Operators
The PHP array operators are used to compare arrays.
PHP Condi onal Assignment Operators
The PHP condi onal assignment operators are used to set a value depending on condi ons:
4. Explain Flow controls and Looping in PHP? With examples.
Condi onal statements are used to perform different ac ons based on different condi ons.
Condi onal statements are flow controls.
PHP Condi onal Statements
In PHP we have the following condi onal statements:
• if statement - executes some code if one condi on is true
• if...else statement - executes some code if a condi on is true and another code if that condi on is
false
• if...elseif...else statement - executes different codes for more than two condi ons
• switch statement - selects one of many blocks of code to be executed
Example:
o/p:
o/p:
o/p:
o/p:
5. Explain arrays and strings in PHP?
An array is a special variable, which can hold more than one value at a me.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look
like this:
An array can hold many values under a single name, and you can access the values by referring to an
index number.
Create an Array in PHP
In PHP, the array() func on is used to create an array:
array();
In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associa ve arrays - Arrays with named keys
• Mul dimensional arrays - Arrays containing one or more arrays
PHP String Func ons:
6. Explain the func ons in PHP?
The real power of PHP comes from its func ons.
PHP has more than 1000 built-in func ons, and in addi on you can create your own custom func ons.
PHP Built-in Func ons
PHP has over 1000 built-in func ons that can be called directly, from within a script, to perform a specific
task.
PHP User Defined Func ons
Besides the built-in PHP func ons, it is possible to create your own func ons.
• A func on is a block of statements that can be used repeatedly in a program.
• A func on will not execute automa cally when a page loads.
• A func on will be executed by a call to the func on.
Create a User Defined Func on in PHP
A user-defined func on declara on starts w ith the word func on:
Syntax
func on func onName (){
code to be exeuted;
A func on name must start with a le er or an underscore. Func on names are NOT case -sensi ve.
In the example below, we create a func on named "writeMsg()". The openi ng curly brace ( { ) indicates
the beginning of the func on code, and the closing curly brace ( } ) indicates the end of the func on. The
func on outputs "Hello world!". To call the func on, just write its name followed by brackets ():
}
7. Explain File Handling and uploading in PHP?
PHP file handling:
File handling is an important part of any web applica on. You o en need to open and process a file for
different tasks.
PHP Manipula ng Files:
PHP has several func ons for crea ng, reading, uploading, and edi ng files.
:
PHP Read File - fread()
The fread() func on reads from an open file.
The first parameter of fread() contains the name of the file to read from and the second parameter
specifies the maximum number of bytes to read.
The following PHP code reads the "webdic [Link]" file to the end:
fread($myfile,filesize("webdic [Link]"));
PHP Close File - fclose()
The fclose() func on is used to close an open file.
It's a good programming prac ce to close all files a er you have finished with them. You don't want an
open file running around on your server taking up resources!
The fclose() requires the name of the file (or a variable that holds the filename) we want to close:
PHP Read Single Line - fgets()
The fgets() func on is used to read a single line from a file.
The example below outputs the first line of the "webdic [Link]" file: Example
PHP Check End-Of-File - feof()
The feof() func on checks if the "end -of-file" (EOF) has been reached.
The feof() func on is useful for looping through data of unknown length.
The example below reads the "webdic [Link]" file line by line, un l end -of-file is reached:
PHP Read Single Character - fgetc()
The fgetc() func on is used to read a single character from a file.
The example below reads the "webdic [Link]" file character by character, un l end-of-file is reached:
PHP Create File - fopen()
The fopen() func on is also used to create a file. Maybe a li le confusing, but in PHP, a file is created
the same func on used to open files.
If you use fopen() on a file that does not exist, it will create it, given that the file is opened for wri ng (w)
or appending (a).
The example below creates a new file called "tes [Link]". The file will b e created in the same directory
where the PHP code resides:
PHP Write to File - fwrite()
The fwrite() func on is used to write to a file.
The first parameter of fwrite() contains the name of the file to write to and the second parameter is the
using string to be wri en.
The example below writes a couple of names into a new file called "newfi[Link]":
PHP Overwri ng
Now that "newfi[Link]" contains some data we can show what happens when we open an exis ng file for
wri ng. All the exis ng data will be ERASED and we start with an empty file.
PHP Append Text
You can append data to a file by using the "a" mode. The "a" mode appends text to the end of the file,
while the "w" mode overrides (and erases) the old content of the file.
PHP File Upload:
With PHP, it is easy to upload files to the server.
However, with ease comes danger, so always be careful when allowing file uploads!
Configure The "[Link]" File
First, ensure that PHP is configured to allow file uploads.
In your "[Link]" file, search for the file_uploads direc ve.
8. Explain E-mail basics in PHP?
PHP Mail Introduc on
The mail() func on allows you to send emails directly from a script.
Requirements
For the mail func ons to be available, PHP requires an installed and working email syst em. The program
to be used is defined by the configura on se ngs in the [Link] file.
Installa on
The mail func ons are part of the PHP core. There is no installa on needed to use these func ons.
Run me Configura on
The behavior of the mail func ons is affected by se ngs in [Link]:
PHP mail() Func on
9. Explain Database with PHP in Detail?
PHP MySQL Database
With PHP, you can connect to and manipulate databases.
MySQL is the most popular database system used with PHP.
MySQL
• MySQL is a database system used on the web
• MySQL is a database system that runs on a server
• MySQL is ideal for both small and large applica ons
• MySQL is very fast, reliable, and easy to use
• MySQL uses standard SQL
• MySQL compiles on a number of pla orms
• MySQL is free to download and use
• MySQL is developed, distributed, and supported by Oracle Corpora on
• MySQL is named a er co-founder Monty Widenius's daughter: My
The data in a MySQL database are stored in tables. A table is a collec on of related data, and it consists of
columns and rows.
Databases are useful for storing informa on categorically. A company may have a database with the
following tables:
• Employees
• Products
• Customers
• Orders
PHP + MySQL Database System
PHP combined with MySQL are cross -pla orm (you can develop in Windows and serve on a Unix
We can query a database for specific informa on and have a recordset returned.
Look at the following query (using standard SQL):
SELECT LastName FROM Employees
The query above selects all the data in the "LastName" column from the "Employees" table.
MySQL is the- de facto standard database system for web sites with HUGE volumes of both data and end
-
Another great thing about MySQL is that it can be scaled down to support embedded database
• pla orm)
Database Queries
A query is a ques on or a request. users (like
Facebook, Twi er, and Wikipedia).
applica ons.
PHP 5 and later can work with a MySQL database using:
• MySQLi extension (the "i" stands for improved)
• PDO (PHP Data Objects) we demonstrate three ways of working with PHP and MySQL:
• MySQLi (object-oriented)
• MySQLi (procedural)
• PDO