Wikis & learning

Read parameters from the intended input channel

A PHP page can receive a query string, form body and cookies in a single request. Read from the channel that matches the purpose of the field so precedence stays explicit.

A PHP page can receive a query string, form body and cookies in a single request. Read from the channel that matches the purpose of the field so precedence stays explicit.

Walkthrough

This example uses a sample array shaped like a query string. A search term must be text before it reaches trim. The page number uses an independent default and a lower bound.

PHP example

<?php
$query = ['q' => '  calendar ', 'page' => '2'];
$term = is_string($query['q'] ?? null) ? trim($query['q']) : '';
$number = filter_var($query['page'] ?? '', FILTER_VALIDATE_INT);
$page = $number === false ? 1 : max(1, $number);
echo $term, ' / page ', $page;

Expected output

calendar / page 2

Change and check

Pass an array instead of text as the search term. The example should choose an empty term and continue without a type warning.