1.2.291. Could Use Compact

Compact() turns a group of variables into an array. It may be used to simplify expressions.

Note that compact accepts any string, and any undefined variable is not set, without a warning.

<?php

$a = 1;
$b = 2;

// Compact call
$array = compact('a', 'b');

$array === [1, 2];

// Detailing all the keys and their value
$array = ['a' => $a, 'b' => $b];

?>

See also compact.

1.2.291.1. Suggestions

  • Replace the array() call with a compact() call.

1.2.291.2. Specs

Short name

Structures/CouldUseCompact

Rulesets

All, Changed Behavior, Suggestions

Exakat since

1.1.6

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

variable

Examples

WordPress

Available in

Entreprise Edition, Exakat Cloud