1.2.277. Could Cast To Array

The array cast operator transform a scalar into an array with that scalar. It also keeps an array as an array, so a single call to (array) is able to convert scalars to array, while keeping values already in array form intact.

<?php

//
if (!is_array($a)) {
     $a = [$a];
}

// equivalent to
$a = (array) $a;

// same, with the else
if (is_array($a)) {
} else {
     $a = array($a);
}

?>

See also Mastering the (array) Cast Operator in PHP.

1.2.277.1. Suggestions

  • Use a direct cast to array

1.2.277.2. Specs

Short name

Structures/CouldCastToArray

Rulesets

All, Changed Behavior, Suggestions

Exakat since

2.6.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

array, cast

Available in

Entreprise Edition, Exakat Cloud