Unit 1: Introduction to PHP programming
Web Programming-I notes · PTU syllabus (UGDSE301)
On this page
Unit summary
HTML pages are static; real websites need code that runs on the server — to process forms, talk to databases and build pages dynamically. PHP is one of the most widely used server-side languages. This unit introduces server-side scripting and the basics of PHP: syntax, variables, constants, data types, operators, control structures, functions, and handling HTML forms.
After this unit you can
- Explain server-side scripting and how PHP works with a web server
- Use PHP variables, constants, data types and operators
- Write PHP control structures and functions
- Process HTML form data with GET and POST
PTU syllabus topics
- Server-side scripting overview
- PHP syntax
- variables
- constants
- data types
- expressions
- operators
- control structures (if, else, switch, loops)
- functions
- handling HTML forms with GET and POST
- embedding PHP into HTML
Topic 1
Server-side scripting
A server-side script runs on the web server and sends only its output, usually HTML, to the browser. The user never sees the code. PHP is a server-side language; JavaScript in the browser is client-side.
PHP runs on the server; the browser receives only the generated HTML, never the PHP source
Runs on
The browser
The web server
Can access database?
No (directly)
Yes
Code visible to user?
Yes
No — only the HTML output is sent
Uses
Validation, interactivity
Logins, database access, dynamic pages
- 1Browser requests page.php
- 2Web server (Apache) passes it to the PHP engine
- 3PHP runs
May query MySQL
- 4Output HTML is sent back
- 5Browser displays it
Example
A login form: JavaScript in the browser checks that the email field is not empty before sending (client-side). PHP on the server then compares the email and password with the database and decides whether to log the user in (server-side). The password check must be on the server, because client-side code can be bypassed.
Exam tip
Never rely on client-side validation alone. Always validate again on the server.
Topic 2
PHP syntax, variables and data types
PHP code sits inside <?php ... ?> tags and statements end with semicolons.
php<?php
$name = "Ana"; // string
$age = 19; // integer
$cgpa = 8.6; // float
$isEnrolled = true; // boolean
define("COLLEGE", "SBS"); // constant
echo "Hello $name from " . COLLEGE; // . joins strings
?>- Variables start with
$, are case-sensitive, and need no type declaration (PHP is loosely typed). - Data types: string, integer, float, boolean, array, object, NULL and resource.
- Operators: arithmetic (+ − × / % **), assignment (=, +=), comparison (==, ===, !=, <, >), logical (&&, ||, !) and string concatenation (.).
Example
A simple PHP page: <?php $name = "Asha"; $marks = 82; echo "Hello $name, your marks are $marks"; ?> prints Hello Asha, your marks are 82.
Exam tip
Variables start with $; double quotes expand variables, single quotes do not.
Topic 3
Control structures and functions
php<?php
$marks = 72;
if ($marks >= 75) echo "Distinction";
elseif ($marks >= 60) echo "First division";
else echo "Pass";
for ($i = 1; $i <= 5; $i++) echo $i;
foreach (["BCA", "BBA"] as $course) echo $course;
function area($l, $b = 1) { // default parameter
return $l * $b;
}
echo area(5, 4); // 20
?>PHP also has while, do…while and switch.
Example
A function with a default argument: function grade($m, $pass = 40) { return $m >= $pass ? "Pass" : "Fail"; } Then grade(55) returns "Pass" and grade(55, 60) returns "Fail".
Topic 4
Handling HTML forms
html<form action="welcome.php" method="post">
Name: <input type="text" name="uname">
<input type="submit">
</form>php<?php
$name = htmlspecialchars($_POST["uname"]); // prevents script injection
echo "Welcome, $name";
?>$_GETreads data sent in the URL;$_POSTreads data sent in the request body;$_REQUESTreads both.
Exam tip
Always validate and sanitise form input on the server (with htmlspecialchars, filter_var and prepared statements) — never trust user input.
Example
Validating input: $name = trim($_POST["name"]); if (empty($name)) { $error = "Name is required"; } elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) { $error = "Invalid email"; } Use htmlspecialchars($name) when printing it back.
Key terms
- Server-side scripting
- Code executed on the web server before the page is sent
- PHP
- A server-side scripting language for dynamic web pages
- Superglobal
- Built-in arrays like $_GET and $_POST available everywhere
- Loosely typed
- Variables don't need a declared type
- htmlspecialchars
- Converts special characters to prevent injection
Quick revision
- PHP runs on the server; only HTML reaches the browser.
- Variables start with $; constants with define().
- . concatenates strings; === checks value and type.
- $_GET for URL data, $_POST for form-body data.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Differentiate between client-side and server-side scripting.
- Q2.How are variables declared in PHP?
- Q3.How do you define a constant in PHP?
- Q4.Differentiate between $_GET and $_POST.
- Q5.What is the use of htmlspecialchars?
Long-answer questions
- Q1.Explain how a PHP page is processed by a web server.
- Q2.Explain PHP data types, operators and control structures with examples.
- Q3.Write a PHP program to process a registration form using POST.
Stuck on this unit?
Message SBS on WhatsApp for help with Web Programming-I, or to ask about studying BCA at Synetic.
