2

Seen in functions where $a is a function parameter:

if(!is_array($a))
    $a=[$a]

I just don't know what this means,

Thanks!

Jackson
  • 264
  • 2
  • 9

3 Answers3

4

It means;

if $a is not an array, then create $a as an array and use the contents (value) of $a as the first element of the newly created array called $a.

In readable English emulating a Code, this could mean:

<?php
    if($a IS NOT AN ARRAY):
        THEN CREATE A NEW VARIABLE $a OF TYPE: ARRAY.
        TAKE WHATEVER IS INSIDE THE INITIAL $a VARIABLE...
        AND PUT IT AS THE FIRST ELEMENT OF THE OVERRIDDEN, NEW $a VARIABLE.
    endif;
Poiz
  • 7,611
  • 2
  • 15
  • 17
2

This code convert $a from some non-array data-type to array data-type

if(!is_array($a))   \\check whether $a is not an array
    $a=[$a]         \\change $a to an array with only one element which was previously stored in $a
bugwheels94
  • 30,681
  • 3
  • 39
  • 60
1

It takes a simple variable and turns it into an array.

[$a] is shorthand for array($a).

If $a is not an array, [$a] turns it into an array.

Martin
  • 22,212
  • 11
  • 70
  • 132
colonelclick
  • 2,165
  • 2
  • 24
  • 33