Unit 1 of 4 · B.Sc IT Sem 4

Unit 1: PHP fundamentals

Programming in PHP notes · PTU syllabus (BSIT401)

3 min read9 topics10 exam questions
On this page
  1. Unit summary
  2. Evolution of PHP
  3. PHP scripting basics
  4. Receiving user input
  5. PHP data types
  6. Operators
  7. Variable manipulation and scope
  8. Control structures: if, elseif and switch
  9. Loops: while, do-while and for
  10. Nesting control structures
  11. Key terms
  12. Quick revision
  13. Important questions

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)
ProcessHow a PHP page is served
  1. 1Browser requests page.php
  2. 2Web server passes it to the PHP engine
  3. 3PHP runs the script

    May query MySQL

  4. 4Output is plain HTML
  5. 5Browser displays the HTML
1

Topic 1

Evolution of PHP

ProcessEvolution of PHP
  1. 1

    1994–95

    Rasmus Lerdorf's Personal Home Page Tools

  2. 2

    1997–98

    PHP 3 rewritten by Zeev Suraski and Andi Gutmans; name becomes PHP: Hypertext Preprocessor

  3. 3

    2000

    PHP 4 with the Zend Engine

  4. 4

    2004

    PHP 5 with full object orientation and PDO

  5. 5

    2015

    PHP 7 — roughly twice as fast, scalar type declarations

  6. 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).
2

Topic 2

PHP scripting basics

ComparisonClient-side vs server-side scripting
Client-side (JavaScript)
Server-side (PHP)

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

ProcessHow a PHP page is served
  1. 1Browser requests page.php
  2. 2Web server (Apache) passes it to the PHP engine
  3. 3PHP runs

    May query MySQL

  4. 4Output HTML is sent back
  5. 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.
3

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.
4

Topic 4

PHP data types

TypeExample
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).
5

Topic 5

Operators

Key termsPHP 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.

6

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.
ComparisonVariable scope
Where declared
Accessible from

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
?>
7

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";
}
?>
8

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.
9

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

  1. Q1.What does PHP stand for?
  2. Q2.Distinguish echo and print.
  3. Q3.Distinguish == and === with an example.
  4. Q4.What is a static variable?
  5. Q5.Distinguish while and do-while loops.
  6. Q6.How do you access a global variable inside a function?

Long-answer questions

  1. Q1.Trace the evolution of PHP and state its advantages.
  2. Q2.Explain PHP data types and operators with examples.
  3. Q3.Explain variable scope in PHP with a program.
  4. 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.

WhatsApp us