Server Side Programming in PHP
1. What is PHP?
PHP (PHP: Hypertext Preprocessor) is an open-source, server-side scripting language embedded in HTML, executed on the web server; only the resulting HTML is sent to the browser. Runs everywhere via the AMP stack (Apache + MySQL + PHP). Files use the .php extension.
| Feature | Detail |
|---|---|
| Server-side | Code runs on the server; browser sees only output |
| Embedded in HTML | <?php ... ?> blocks inside .php pages |
| Loosely typed | x = "hi"; — types auto-converted |
| Cross-platform & free | Windows/Linux/Apache/IIS/Nginx |
| Database support | MySQL/MariaDB (mysqli, PDO), PostgreSQL, Oracle |
| Large ecosystem | Sessions, cookies, file upload, mail, GD images, CMSs (WordPress, Drupal) |
2. Basic Syntax
<?php
echo "Hello, World!"; // output
$name = "Asha"; $marks = 92; // variables start with $
echo "<p>$name scored $marks</p>"; // interpolation in double quotes
/* multi-line comment */
# single-line comment too
?>
Data types: int, float, string, bool, array, object, NULL. String functions: strlen, strtoupper, substr, trim, explode, implode, strcmp, str_replace. Math: abs, round, rand, sqrt, max, min.
if ($marks >= 90) echo "Distinction";
elseif ($marks >= 60) echo "First class";
else echo "Pass";
for ($i = 1; $i <= 5; $i++) echo $i;
foreach ($colors as $c) echo $c;
function area($w, $h) { return $w * $h; } // user-defined function
$marks = array(88, 92, 75); // indexed array
$student = array("roll" => 12, "name" => "Asha"); // associative array
echo $student["name"]; sort($marks); count($marks);
<!-- welcome.php -->
<?php
$nm = $_POST["nm"]; $em = $_POST["em"];
echo "Welcome, $nm ($em)";
?>
_POST → values in the HTTP body (no length limit, not shown in URL, preferred for sensitive data); $_REQUEST → either.
// cookie: stored on the client
setcookie("user", "Asha", time() + 3600); // 1 hour
echo $_COOKIE["user"];
// session: stored on the server; id kept in a cookie
session_start();
$_SESSION["roll"] = 12;
echo $_SESSION["roll"];
session_destroy(); // logout
7. Other Practical Features
- File upload:
<form enctype="multipart/form-data">→move_uploaded_file($_FILES['pic']['tmp_name'], "uploads/pic.jpg"). - File read/write:
fopen, fread, fwrite, fclose, file_get_contents. - E-mail:
mail(subject, headers). - Include files:
include 'header.php'; require 'db.php';. - Header/redirect:
header("Location: login.php");.
8. PHP vs Client-side JavaScript vs JSP
| Aspect | PHP | JavaScript | JSP |
|---|---|---|---|
| Runs on | Server | Browser | Server (Java) |
| Compiled? | Interpreted (op-cached) | Interpreted | Compiled to servlet bytecode |
| DB access | Yes | No | Yes (JDBC) |
| Best for | Rapid web apps | UI/validation | Enterprise Java apps |
9. Exam Points
- Explain the features of PHP with syntax examples — 7-10 marks.
- Write a PHP program to handle a form (login / registration) with
$_POST— 10 marks (very frequent program). - Write PHP+MySQL code to insert/display records — 10 marks.
- Cookies vs sessions in PHP with code — 5 marks.
- GET vs POST handling in PHP — 5 marks.