CHAPTER 2
Introduction to Server-Side Scripting (PHP I)
Master PHP fundamentals: client-server model, variables, control structures, and arrays.
5
Sections
Client-Server · PHP Basics · Variables · Control Structures · Arrays
20
Flashcard Terms
Key vocabulary and syntax patterns from the chapter
15
Quiz Questions
Test your understanding with code-tracing and concept questions
Client-Server Request-Response Cycle
🖥
User enters URL
CLIENT
→
📤
HTTP GET Request
CLIENT
→
🔍
Server looks up file
SERVER
→
⚙️
PHP Interpreter runs
SERVER
→
📥
Browser renders HTML
CLIENT
Key Concepts at a Glance
What is the difference between a static and dynamic web page?▼
A static web page (HTML/CSS only) returns the same content to every user at any time. A dynamic web page uses server-side scripting (like PHP) to generate different content based on logic — for example, showing "Good Morning" or "Good Afternoon" depending on the time of day.
How does a PHP file differ from an HTML file?▼
An HTML file is returned directly to the browser. A PHP file is first processed by the PHP Interpreter on the server — it executes any code between
<?php and ?> tags, then sends a resulting HTML file back to the client. The browser never sees PHP code.What is the role of the PHP Interpreter?▼
The PHP Interpreter is software installed on the web server. It reads a
.php file, executes all PHP code blocks, copies all non-PHP code directly, and produces a resulting .html output that is sent back to the client.What are the 4 data types in PHP?▼
Integer — whole numbers (e.g., 0, 1, -2, 1000)
Float — real/decimal numbers (e.g., 1.23, -2.0, 3.14)
Boolean — True or False (no quotes!)
String — text enclosed in double quotes (e.g., "Hello World")
Float — real/decimal numbers (e.g., 1.23, -2.0, 3.14)
Boolean — True or False (no quotes!)
String — text enclosed in double quotes (e.g., "Hello World")
What is the difference between an array and an associative array?▼
A regular array uses numeric indexes starting from 0 (e.g.,
$friends[0]). An associative array uses string indexes (keys) that you define (e.g., $user["name"]). Associative arrays are used to store related attributes of an entity.CORE CONCEPTS
PHP Fundamentals
Explore the building blocks of PHP server-side scripting.
PHP File Syntax
PHP code is enclosed between
<?php and ?>. Every statement ends with a semicolon. Non-PHP code (HTML) is copied directly to output.<?php
echo "Hello World!";
?>
Variables & Data Types
Variables start with
$. Four data types: Integer, Float, Boolean, String.$name = "Alice";
$age = 18;
$score = 95.5;
$pass = True;
String Operations
Use
. to concatenate strings. Use {$var} inside double-quoted strings for variable parsing. Escape special chars with \.$n = "Bob";
echo "Hello {$n}!";
// Hello Bob!
If-Else & Elseif
Conditional logic uses
if(), else, and elseif(). Blocks must be enclosed in { }. Use == to compare, not =.if($hour < 12){
echo "Morning";
} else {
echo "Afternoon";
}
Loops: while & for
while repeats while condition is true. for loops define initialise, condition, and afterthought in one line. Use foreach for arrays.for($i=1; $i<=5; $i++){
echo "{$i}<br>";
}
Arrays & Associative Arrays
Arrays store multiple values. Add with
$arr[] = val. Get length with count(). Associative arrays use string keys.$user["name"] = "Ken";
$user["age"] = 16;
echo $user["name"];
Arithmetic Operators
Standard operators:
+ - * /. Modulo % returns remainder. Exponentiation **. Use floor() for floor division.$r = 9 % 4; // 1
$p = 2 ** 8; // 256
$f = floor(9/4); // 2
Comments in PHP
Single-line comments use
// or #. Multi-line comments use /* ... */. HTML comments outside PHP tags are copied to output.// Single-line comment
/* Multi-line
comment */
<?php $x = 5; ?>
REFERENCE
PHP Cheat Sheet
Quick reference for syntax, operators, and built-in functions covered in Chapter 2.
Variables & Data Types
| Concept | Syntax / Example | Notes |
|---|---|---|
| Declare variable | $name = "Alice"; | Must start with $, then _ or letter |
| Integer | $n = 42; | Whole numbers, positive or negative |
| Float | $pi = 3.14; | Decimal / real numbers |
| Boolean | $ok = True; | No quotes — True/False |
| String | $s = "hello"; | Use double quotes "" |
Operators
| Operator | Example | Result / Meaning |
|---|---|---|
| + - * / | $c = $a + $b; | Arithmetic |
| % | 9 % 4 → 1 | Modulo (remainder) |
| ** | 2 ** 8 → 256 | Exponentiation |
| += -= | $a += $b; | Combined assignment |
| ++ -- | $a++; | Increment / Decrement |
| . | "Hi" . $name | String concatenation |
| == != < > <= >= | $x == $y | Relational operators |
| and / && | or / || ! | $a and $b | Logical operators |
Control Structures
| Structure | PHP Syntax | Notes |
|---|---|---|
| if-else | if($x > 0){ ... } else { ... } | Condition must be boolean expression |
| elseif | elseif($x == 0){ ... } | Checks additional conditions |
| while loop | while($i < 10){ $i++; } | Checks condition before each iteration |
| for loop | for($i=0; $i<5; $i++){ ... } | Init; condition; afterthought |
| foreach loop | foreach($arr as $v){ ... } | Iterates over array elements |
Arrays
| Operation | Code | Notes |
|---|---|---|
| Create empty | $arr = []; | Empty array |
| Create with values | $arr = ["a","b","c"]; | Index starts from 0 |
| Access element | $arr[1] | Returns "b" |
| Add element | $arr[] = "d"; | Appends to end |
| Get length | count($arr) | Returns number of elements |
| Associative array | $user["name"] = "Ken"; | String index / key |
String Escaping
| Character | Escape | Example | Output |
|---|---|---|---|
| " (double quote) | \" | echo "It is \"great\""; | It is "great" |
| \ (backslash) | \\ | echo "A\\B"; | A\B |
| $ (dollar sign) | \$ | echo "Cost: \$50"; | Cost: $50 |
MEMORISE
Flashcards
Click a card to reveal the definition. Use the arrows to navigate.
1 / 12
TERM / CONCEPT
Client
↕ Click to flip
DEFINITION
Loading...
PRACTICE
Quiz
Test your PHP knowledge with code-tracing and concept questions.
Q1 / 15
Score: 0