Unit 2: Arrays, strings and file handling
Web Programming-I notes · PTU syllabus (UGDSE301)
On this page
Unit summary
Real applications deal with lists of data, text and files, and need to remember users between pages. This unit covers PHP arrays and array functions, string manipulation, file handling and uploads, and maintaining state with cookies and sessions.
After this unit you can
- Create indexed, associative and multidimensional arrays and use array functions
- Manipulate and format strings
- Read, write, append and delete files and handle file uploads
- Use cookies and sessions to maintain state
PTU syllabus topics
- Indexed
- associative and multidimensional arrays
- array functions (sorting, merging, searching)
- string manipulation and formatting functions
- file handling (open, read, write, append, delete)
- file uploads
- cookies and sessions
Stored on
User's browser
Server
Security
Visible and editable by the user
Safer; only an ID sits in the browser
Lifetime
Until expiry date
Until logout or timeout
Typical use
Remember preferences
Keep a user logged in
Topic 1
Arrays
Indexed
$c = ["BCA", "BBA", "MBA"];
$c[0]
Associative
$s = ["name" => "Ana", "roll" => 12];
$s["name"]
Multidimensional
$m = [[1,2],[3,4]];
$m[1][0] gives 3
| Function | Purpose |
|---|---|
| count($a) | Number of elements |
| sort / rsort | Sort values ascending / descending |
| asort / ksort | Sort associative arrays by value / key |
| array_merge($a, $b) | Join arrays |
| in_array($x, $a) | Check if a value exists |
| array_search($x, $a) | Return the key of a value |
| array_push / array_pop | Add / remove at the end |
Topic 2
String functions
| Function | Purpose |
|---|---|
| strlen($s) | Length |
| strtoupper / strtolower | Change case |
| str_replace($a, $b, $s) | Replace text |
| strpos($s, $x) | Position of a substring |
| substr($s, $start, $len) | Extract part of a string |
| trim($s) | Remove surrounding spaces |
| explode(",", $s) / implode(",", $a) | Split into an array / join an array |
| sprintf / number_format | Format strings and numbers |
Topic 3
File handling and uploads
php<?php
$f = fopen("notes.txt", "a"); // r, w, a, r+
fwrite($f, "New line\n");
fclose($f);
echo file_get_contents("notes.txt"); // read whole file
if (file_exists("old.txt")) unlink("old.txt"); // delete
?>File upload: the form needs method="post" and enctype="multipart/form-data"; PHP reads the file from $_FILES["file"] and saves it with move_uploaded_file(). Check the file type and size before saving.
Topic 4
Cookies and sessions
HTTP is stateless — each request is independent — so websites use cookies and sessions to remember users.
Stored on
The user's browser
The server
Security
Less secure (user can see and edit)
More secure
Size
Small (about 4 KB)
Larger
Lifetime
Until its expiry time
Until logout or timeout
PHP
setcookie(), $_COOKIE
session_start(), $_SESSION
php<?php
session_start();
$_SESSION["user"] = "ana"; // store
echo $_SESSION["user"]; // read on any page after session_start()
session_destroy(); // logout
?>Key terms
- Associative array
- An array with named keys
- explode
- Splits a string into an array
- enctype
- Form attribute needed for file uploads
- Cookie
- Small data stored in the browser
- Session
- User data stored on the server across pages
Quick revision
- Indexed, associative and multidimensional arrays.
- explode splits, implode joins.
- fopen modes: r, w, a, r+; uploads need multipart/form-data.
- Sessions are server-side and more secure than cookies.
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 indexed and associative arrays.
- Q2.What does explode() do?
- Q3.Which enctype is needed for file uploads?
- Q4.Differentiate between cookies and sessions.
- Q5.How do you destroy a session?
Long-answer questions
- Q1.Explain arrays in PHP and their important functions with examples.
- Q2.Write a PHP program to upload a file and validate its type and size.
- Q3.Explain cookies and sessions with programs, and compare them.
Stuck on this unit?
Message SBS on WhatsApp for help with Web Programming-I, or to ask about studying BCA at Synetic.
