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
- 1Request
Browser asks for page.php
- 2Execute
Server runs the PHP code
- 3Generate
PHP outputs HTML
- 4Respond
Browser receives plain HTML
Topic 1
Server-side scripting
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
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 (.).
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.
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.
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.
