1.2.1385. Useless Global

Global are useless in two cases. First, on super-globals, which are always globals, like $_GET; secondly, on variables that are not used.

Also, PHP has superglobals, a special team of variables that are always available, whatever the context. They are : $GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST and $_ENV.

<?php

// $_POST is already a global : it is in fact a global everywhere
global $_POST;

// $unused is useless
function foo() {
    global $used, $unused;

    ++$used;
}

?>

1.2.1385.1. Suggestions

  • Drop the global expression

1.2.1385.2. Specs

Short name

Structures/UselessGlobal

Rulesets

All, Analyze

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

super-global, $_get, $_post, $_server, $globals, $_files, $_cookie, $_request, $_env

Examples

Zencart, HuMo-Gen

Available in

Entreprise Edition, Exakat Cloud