OTARIKI OTARIKI - 1 month ago 11
PHP Question

Clearing _POST array fully

I want clear $_POST array content fully, all examples what I see in internet, looks like this:

if (count($_POST) > 0) {
foreach ($_POST as $k=>$v) {
unset($_POST[$k]);
}
}


Tell please, this variant will be not more better? (Point of view as saving resources)

if (count($_POST) > 0) {
$_POST = array();
}


or not ?

Answer

Yes, that is fine. $_POST is just another variable, except it has (super)global scope.

$_POST = array();

...will be quite enough. The loop is useless. It's probably best to keep it as an array rather than unset it, in case other files are attempting to read it and assuming it is an array.