I have a variable $v that can be either single string or array of strings
and I have a code:
$a = array();
if (is_array($v)) {
$a = $v;
} else {
$
As others have said, casting a scalar value to an array will produce a singleton array (i.e. an array with the scalar as its only element). However, as still others have pointed out, take care to only do this if you know the value is going to be a scalar and not a class instance.
From the PHP docs:
For any of the types
integer,float,string,booleanandresource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words,(array)$scalarValueis exactly the same asarray($scalarValue).If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.