Unit 1: PHP fundamentals
Programming in PHP notes · PTU syllabus (BSIT401)
On this page
Unit summary
PHP powers a large share of the web's dynamic pages. This unit covers the evolution of PHP, scripting basics, receiving user input, data types, operators, variable manipulation and scope, and control structures — if/elseif, switch, while, do-while, for loops and nesting.
After this unit you can
- Trace the evolution of PHP and explain how a PHP script runs
- Receive user input from forms
- Use data types, operators and variable scope
- Write decision and loop structures, including nested loops
PTU syllabus topics
- Evolution of PHP
- PHP scripting basics
- receiving user input
- PHP data types
- operators
- variable manipulation and scope
- control structures (if/elseif, switch, while, do-while, for loops, nesting)
- 1Browser requests page.php
- 2Web server passes it to the PHP engine
- 3PHP runs the script
May query MySQL
- 4Output is plain HTML
- 5Browser displays the HTML
Topic 1
Evolution of PHP
- 1
1994–95
Rasmus Lerdorf's Personal Home Page Tools
- 2
1997–98
PHP 3 rewritten by Zeev Suraski and Andi Gutmans; name becomes PHP: Hypertext Preprocessor
- 3
2000
PHP 4 with the Zend Engine
- 4
2004
PHP 5 with full object orientation and PDO
- 5
2015
PHP 7 — roughly twice as fast, scalar type declarations
- 6
2020 onward
PHP 8 — JIT compiler, named arguments, match expression
- Why PHP: free and open source, runs on all major servers and operating systems, embeds easily in HTML, strong MySQL support, large community and frameworks (Laravel, WordPress, Drupal).
Topic 2
PHP scripting basics
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
php<?php
// single-line comment
/* multi-line
comment */
echo "Hello, SBS!"; // echo outputs text
print "<br>Welcome"; // print also outputs (returns 1)
?>- A PHP file has the .php extension and can mix HTML with PHP blocks; it needs a server such as Apache (XAMPP, WAMP) to run.
Topic 3
Receiving user input
html<form method="post" action="add.php">
First number: <input type="number" name="a">
Second number: <input type="number" name="b">
<input type="submit" value="Add">
</form>php<?php
// add.php
$a = (int) $_POST["a"];
$b = (int) $_POST["b"];
echo "Sum = " . ($a + $b);
?>- $_GET for values in the URL (search boxes), $_POST for form bodies (logins, registrations), isset() to check that a value was sent.
Topic 4
PHP data types
| Type | Example |
|---|---|
| Integer | $roll = 21; |
| Float (double) | $fee = 45500.50; |
| String | $name = "Simran"; |
| Boolean | $paid = true; |
| Array | $marks = [78, 85, 90]; |
| Object | $s = new Student(); |
| NULL | $x = null; |
| Resource | $f = fopen("a.txt", "r"); |
- Type functions: gettype(), var_dump(), is_int(), is_string(); casting with (int), (float), (string).
Topic 5
Operators
- Arithmetic
- + − × / %, and the exponent operator (two asterisks) for power
- Assignment
- = += −= .=
- Comparison
- == (equal value), === (equal value and type), != , <> , < , > , <=> (spaceship)
- Logical
- && (and), the double-bar operator (or), ! (not), and the keywords and, or, xor
- Increment and decrement
- ++$x, $x++, −−$x, $x−−
- String
- . (concatenation), .= (append)
- Null coalescing
- $a ?? "default"
Example
"5" == 5 is true but "5" === 5 is false, because the types differ.
Topic 6
Variable manipulation and scope
- Variables: begin with $, are case-sensitive and take the type of the value assigned (loosely typed).
- Variable variables: $var = "city"; $var = "Ludhiana"; creates $city.
- Constants: define("PI", 3.14) or const PI = 3.14; no $ sign; cannot change.
Local
Inside a function
Only inside that function
Global
Outside all functions
Outside functions; inside one only with the global keyword or $GLOBALS
Static
Inside a function with static
That function; keeps its value between calls
php<?php
$count = 10;
function show() {
global $count; // bring global into local scope
static $calls = 0; // keeps value between calls
$calls++;
echo "Count $count, call $calls<br>";
}
show(); show(); // call 1, call 2
?>Topic 7
Control structures: if, elseif and switch
php<?php
$marks = 68;
if ($marks >= 80) $grade = "A";
elseif ($marks >= 60) $grade = "B";
elseif ($marks >= 40) $grade = "C";
else $grade = "F";
$day = "Sat";
switch ($day) {
case "Sat":
case "Sun": echo "Weekend"; break;
default: echo "Weekday";
}
?>Topic 8
Loops: while, do-while and for
php<?php
$i = 1;
while ($i <= 5) { echo $i; $i++; } // entry-controlled
$j = 1;
do { echo $j; $j++; } while ($j <= 5); // exit-controlled: runs at least once
for ($k = 2; $k <= 20; $k += 2) echo $k . " "; // even numbers
?>- break exits a loop; continue skips to the next iteration.
Topic 9
Nesting control structures
php<?php
// multiplication table 1 to 5 in an HTML table
echo "<table border='1'>";
for ($r = 1; $r <= 5; $r++) {
echo "<tr>";
for ($c = 1; $c <= 5; $c++) {
echo "<td>" . ($r * $c) . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>Key terms
- PHP
- Hypertext Preprocessor, a server-side scripting language
- Superglobal
- Built-in array such as $_POST available in every scope
- Loosely typed
- Variable type decided by the value assigned
- Static variable
- Function variable that keeps its value between calls
- Nested loop
- Loop placed inside another loop
Quick revision
- PHP history: PHP/FI → PHP 3 → 4 (Zend) → 5 (OOP) → 7 (speed) → 8 (JIT).
- Code in <?php ?>; echo and print; comments.
- Input through $_GET, $_POST; isset().
- Eight data types; == vs ===; concatenation with a dot.
- Local, global, static scope; if/elseif, switch, while, do-while, for, nesting.
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.What does PHP stand for?
- Q2.Distinguish echo and print.
- Q3.Distinguish == and === with an example.
- Q4.What is a static variable?
- Q5.Distinguish while and do-while loops.
- Q6.How do you access a global variable inside a function?
Long-answer questions
- Q1.Trace the evolution of PHP and state its advantages.
- Q2.Explain PHP data types and operators with examples.
- Q3.Explain variable scope in PHP with a program.
- Q4.Explain the control structures of PHP with programs.
Stuck on this unit?
Message SBS on WhatsApp for help with Programming in PHP, or to ask about studying B.Sc IT at Synetic.
