Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.

Typecasting

Josh edited this page Aug 6, 2017 · 1 revision

This class contains robust type-conversion functionality. These methods are less performant than native operations like $foo = (int) $foo, but are more intelligent and less likely to explode when passed wacky values.

Use:
\blobfolio\common\cast (by value)
\blobfolio\common\ref\cast (by reference)

array_type()

This function attempts to classify the indexing method of an array.

Arguments

Type Description Notes
array Array. By reference.

Returns

  • "sequential" if the keys are numeric and in sequence beginning with 0
  • "indexed" if the keys are otherwise numeric
  • "associative" if the keys are mixed or non-numeric
  • FALSE if the array is empty

Example

$arr1 = array(
    'apples',
    'bananas',
    'carrots'
);

echo blobfolio\common\cast::array_type($arr1); // sequential

to_array()

Typecast to array. This is like (array) hinting, but will set non-arrayable objects as empty arrays.

Versions

  • By Value
  • By Reference

Aliases

The following are available under PHP7+:

  • ::array()

Arguments

Type Description
mixed Value.

Returns

Returns an array if passed by value, otherwise TRUE if by reference.

Example

// By value.
$foo = \blobfolio\common\cast::to_array('apples'); // ['apples']
$foo = \blobfolio\common\cast::to_array(null); // []
$foo = \blobfolio\common\cast::to_array(array('apples')); // ['apples']

// By reference.
\blobfolio\common\ref\cast::to_array($foo);

to_bool()

Typecast to boolean. This is like (bool) hinting, but will also properly interpret things like "true", "on", etc.

Versions

  • By Value
  • By Reference

Aliases

The following are available under PHP7+:

  • ::bool()
  • ::boolean()

Arguments

Type Description Notes Default
mixed, array Value. If an array is passed, each value will be processed recursively.
bool Flatten. TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. FALSE

Returns

Returns TRUE or FALSE.

Example

// By value.
$foo = \blobfolio\common\cast::to_bool(true); // TRUE
$foo = \blobfolio\common\cast::to_bool(1); // TRUE
$foo = \blobfolio\common\cast::to_bool("true"); // TRUE

// By reference.
\blobfolio\common\ref\cast::to_bool($foo);

to_float()

Typecast to float. This is like (float) hinting, but will strip out non-numeric data and attempt to convert values like percents and US cents.

Versions

  • By Value
  • By Reference

Aliases

The following are available under PHP7+:

  • ::double()
  • ::float()

Arguments

Type Description Notes Default
mixed, array Value. If an array is passed, each value will be processed recursively.
bool Flatten. TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. FALSE

Returns

Returns a float if passed by value, otherwise TRUE.

Example

// By value.
$foo = \blobfolio\common\cast::to_float('$1.00'); // 1
$foo = \blobfolio\common\cast::to_float('10%'); // .1
$foo = \blobfolio\common\cast::to_float(5.5); // 5.5

// By reference.
\blobfolio\common\ref\cast::to_float($foo);

to_int()

Typecast to integer. This is like (int) hinting, but again, will strip out non-numeric data and attempt to convert values like percents, etc. This function will also properly interpret things like "true", "on", etc.

Versions

  • By Value
  • By Reference

Aliases

The following are available under PHP7+:

  • ::int()
  • ::integer()

Arguments

Type Description Notes Default
mixed, array Value. If an array is passed, each value will be processed recursively.
bool Flatten. TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. FALSE

Returns

Returns an integer if passed by value, otherwise TRUE.

Example

// By value.
$foo = \blobfolio\common\cast::to_int(5.5); // 5
$foo = \blobfolio\common\cast::to_int('3'); // 3
$foo = \blobfolio\common\cast::to_int(8); // 8

// By reference.
\blobfolio\common\ref\cast::to_int($foo);

to_number()

This strips out non-numerical data and returns a float.

Versions

  • By Value
  • By Reference

Aliases

The following are available under PHP7+:

  • ::number()

Arguments

Type Description Notes Default
mixed, array Value. If an array is passed, each value will be processed recursively.
bool Flatten. TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. FALSE

Returns

Returns a float if passed by value, otherwise TRUE.

Example

// By value.
$foo = \blobfolio\common\cast::to_number('5.5'); // 5.5
$foo = \blobfolio\common\cast::to_number(3); // 3
$foo = \blobfolio\common\cast::to_number('8%'); // 0.08

// By reference.
\blobfolio\common\ref\cast::to_number($foo);

to_string()

This casts to a UTF-8 string.

Versions

  • By Value
  • By Reference

Aliases

The following are available under PHP7+:

  • ::string()

Arguments

Type Description Notes Default
mixed, array Value. If an array is passed, each value will be processed recursively.
bool Flatten. TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. FALSE

Returns

Returns a string if passed by value, otherwise TRUE.

Example

// By value.
$foo = \blobfolio\common\cast::to_string(1); // 1

// By reference.
\blobfolio\common\ref\cast::to_string($foo);

to_type()

This typecasts the variable to the specified type, mapping to one of the above functions.

Versions

  • By Value
  • By Reference

Arguments

Type Description Notes Default
mixed, array Value. If an array is passed, each value will be processed recursively.
string Type. Either "boolean", "bool", "integer", "int", "double", "float", "string", or "array"
bool Flatten. TRUE overrides the auto-recursive behavior, making sure that only a single value is returned. FALSE

Returns

Returns the cast variable if passed by value, otherwise TRUE.

CLI

Constants

Dom Helpers

Files and Paths

Formatting

General Data Helpers

Images

Multi-Byte Wrappers

Sanitizing and Validation

Typecasting

Clone this wiki locally