PHP Web Basics Tutorial

Why PHP for Learning Web Development?

PHP was designed specifically for the web. Unlike general-purpose languages adapted for web use, PHP:

This transparency makes HTTP concepts visible in ways frameworks obscure.

Tutorial Modules

01. Hello World

Understand the request-response cycle. See what happens when PHP runs on the server.

  • Basic echo output
  • Mixing PHP and HTML
  • PHP as a templating language
Start Module

02. Form Handling

Read user input from forms. Learn about GET vs POST and security essentials.

  • $_GET and $_POST
  • Checking request method
  • Input validation
  • XSS prevention
Start Module

03. Headers & Metadata

Work with HTTP headers. Build APIs and handle different content types.

  • $_SERVER superglobal
  • Content negotiation
  • JSON APIs
  • Reading raw request body
Start Module

04. Sessions

Maintain state across requests. Build login/logout flows.

  • session_start()
  • $_SESSION array
  • Protected pages
  • Security best practices
Start Module

Running the Demos

Local Development: Use PHP's built-in server:
cd php-tutorial
php -S localhost:8000

Then visit http://localhost:8000/

PHP Quick Reference

Superglobals

Variable Contains
$_GET URL query parameters (?key=value)
$_POST Form data from POST requests
$_COOKIE Cookie values
$_SESSION Session data (after session_start())
$_SERVER Server and request metadata
$_FILES Uploaded file information

Essential Functions

// Output
echo "Hello";                              // Output string
print_r($array);                           // Debug array

// Security
htmlspecialchars($str, ENT_QUOTES, 'UTF-8')  // Escape HTML (XSS prevention)

// Headers (must be before any output!)
header('Content-Type: application/json');    // Set content type
header('Location: /other.php');              // Redirect
http_response_code(404);                     // Set status code

// Sessions
session_start();                             // Start/resume session
$_SESSION['key'] = 'value';                 // Store in session
session_regenerate_id(true);                 // Security: new session ID
session_destroy();                           // End session

// JSON
json_encode($data);                          // PHP array to JSON string
json_decode($json, true);                    // JSON string to PHP array

Back to CSE 135 Home | Start Tutorial