Codectionary / Developer documentation / PHP

PHP Request Data

PHP exposes HTTP request data through superglobal arrays such as $_GET and $_POST. Data from a request is untrusted and should be validated before use.

Syntax

$query = $_GET["q"] ?? "";
$clean = trim($query);

Examples

Reading an optional query value

A small, runnable example of this syntax.

<?php
$query = $_GET["q"] ?? "";
$query = trim($query);

if ($query !== "") {
  echo htmlspecialchars($query, ENT_QUOTES, "UTF-8");
}
?>

Best practices

  • Validate input for its expected shape and escape output for its destination; never treat request data as safe by default.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Server-side web applications
File extension
.php
Runs in
PHP runtime on a server or CLI
Usually used with
HTML, databases and web servers

Specifications & further reading

Related PHP documentation