5. Server-Side Scripting (PHP)
5. Server-Side Scripting (PHP)
Total 15
2
Introduction to PHP
Introduction to PHP
• PHP (Hypertext Preprocessor)
• Open source scripting language.
• Suited for web development
• Can be embedded into HTML.
• PHP runs on various platforms (Linux, Unix, Mac OS X,
Windows etc.).
• PHP is compatible with almost all servers used today
• For eg. XAMMP, Apache, NGINX, lighttpd
• Supports a wide range of databases.
• PHP is free
• One can download it from the official PHP resource: www.
php.net.
• PHP is easy to learn and runs efficiently on the server side.
Server Side Scripting Scripting Language
Web Server
Server Side Scripting
• A server is a computer system that serves as a central
repository of data and programs and is shared by the clients.
• The server-side environment that runs a scripting language is
termed web server.
• A user's request is fulfilled by running a script directly on the
web server to generate dynamic HTML pages.
• This HTML is then sent to the client browser.
• It is usually used to provide interactive web sites that
interfaces with databases or other data stores on the server.
Features of PHP
• Simple
• Interpreted
• Faster
• Open Source
• Platform Independent
• Case Sensitive
• Error Reporting
• Real-Time Access
• Monitoring
• Loosely Typed Language
First sample code of PHP
First sample code of PHP
How to execute PHP program
• Type the above program and save it as"first.php"using
any text editor (for e.g. notepad, gedit).
• Local
• Global
• Static
• 1) String
• 2) Integer
• 3) Float
• 4) Boolean
• 5) Array
• 6) NULL
• Loop Structure in PHP : Loops are used to execute the same block
of code repeatedly as long as a certain condition is satisfied.
• Syntax :
• for(initialisation; condition; incrementation or decrementation) {
Statement;
}
Control structures in PHP
Output:-
Control structures in PHP
• Use of foreach loop : This loop works only on arrays, and is used
to loop through each key/value pair in an array.
Output:-
PHP Arrays
● In the example, we store subject in an array at following index
location.
$subjects[0] = "English";
$subjects[1] = "Hindi";
$subjects[2] = "Marathi";
Note : An array can hold many values under a single name, and you
can access the values by referring to an index number.
PHP Associative Arrays
Associative arrays are arrays that use named keys instead of index to
identify record/value. Let us see how to create associative array.
Syntax : $a = array( key1 => value1, key2 =>value2, ...,key n => value n)
PHP Associative Arrays
Output:-
PHP Associative Arrays
In above example, the values of ‘student_mark’ array are stored in
following way:
$student_mark['English'] = "75"
$student_mark['Hindi'] = "64"
$student_mark['Marathi'] = "88"
Instead of index location, we can refer values with the help of keys.
Output:-
PHP Multi-dimensional Arrays
1 Munna 35
2 Guddu 40
3 Bablu 70
PHP Multi-dimensional Arrays
PHP Multi-dimensional Arrays
Output:-
PHP Form Handling
● When the user fills out the form above and clicks the submit button,
the form data is sent for processing to a PHP file named
"welcome.php".
● The form data is sent with the HTTP POST method.
PHP Form Handling
The code for "welcome.php" looks like this:
Output:-
PHP Form Handling
$_POST["email"] helps us to get the data of form field whose name
attribute is email.
Name gender
Now write following code in ‘admission. php’ file.
Output:-
● In above example, PHP Data Object class, i.e. PDO(),
helps us to connect PHP code with almost any database
server.
● It doesn't account for database-specific syntax, and one
can switch database simply by switching the
connection string in many instances.
● It provides database access layer providing a uniform
method of access to multiple databases.
1) Create database connection object
$conn = new
PDO($servername,$username,
$password);
● A cookie is a small text file that the server sends on the user's
computer. It is used to identify user or its machine and track activities
created on the user computer.
● When browser requests server page, cookies are sent along with the
request.
● PHP can be used both to create and retrieve cookie values.
● Eg. Cookies store visited page on browser to optimise search.
Cookies and session in PHP :
Session :