PHP Type Casting In -

PHP Type Casting In

Type Casting refers to converting a given value or variable from one type to another. This is useful when you need to manipulate data types in your program.

Boolean Type Casting

To convert a value into a boolean, prepend the value with (bool) or (boolean). Even if you don’t prepend, PHP will treat the value as boolean based on its content.

Values that are false when converted to boolean:

  • Boolean FALSE
  • Integer or float 0
  • null
  • Empty strings '' or '0'
  • Any undefined variable
  • Empty array array() or []
  • Empty object

Example:

<?php
  echo var_dump((bool) "")."<br>";     // Output: bool(false)
  echo var_dump((bool) 1)."<br>";      // Output: bool(true)
  echo var_dump((bool) -2)."<br>";     // Output: bool(true)
  echo var_dump((bool) "foo")."<br>";  // Output: bool(true)
  echo var_dump((bool) 2.3e5)."<br>";  // Output: bool(true)
  echo var_dump((bool) array(12))."<br>"; // Output: bool(true)
  echo var_dump((bool) array())."<br>";   // Output: bool(false)
  echo var_dump((bool) "false")."<br>";   // Output: bool(true)
?>

Output:

bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)

Integer Type Casting

To convert a value to an integer, prepend it with (int) or (integer).

Example:

<?php
    echo var_dump((int) "1")."<br>";  // Output: int(1)
    echo var_dump((int) 1)."<br>";    // Output: int(1)
    echo var_dump((int) '1')."<br>";  // Output: int(1)
    echo var_dump((int) 2.5)."<br>";  // Output: int(2)
?>

Output:

int(1)
int(1)
int(1)
int(2)

Float Type Casting

To convert a value to a float, prepend it with (float).

Example:

<?php
    echo var_dump((float) "1.5")."<br>"; // Output: float(1.5)
    echo var_dump((float) 1)."<br>";     // Output: float(1)
    echo var_dump((float) '1')."<br>";   // Output: float(1)
?>

Output:

float(1.5)
float(1)
float(1)

String Type Casting

To convert a value to a string, prepend it with (string).

Example:

<?php
    echo var_dump((string) true)."<br>";  // Output: string(1) "1"
    echo var_dump((string) false)."<br>"; // Output: string(0) ""
    echo var_dump((string) 0)."<br>";    // Output: string(1) "0"
    echo var_dump((string) 2.5)."<br>";  // Output: string(3) "2.5"
?>

Output:

string(1) "1"
string(0) ""
string(1) "0"
string(3) "2.5"

Array Type Casting

To convert a value to an array, prepend it with (array).

Example:

<?php
    // Define variables and convert them to arrays
    $int_var = (array)10;
    $string_var = (array)"String";
    $bool_var = (array)True;

    var_dump($int_var);  // Output: array(1) { [0]=> int(10) }
    echo "<br>";
    var_dump($string_var); // Output: array(1) { [0]=> string(6) "String" }
    echo "<br>";
    var_dump($bool_var);   // Output: array(1) { [0]=> bool(true) }
?>

Output:

array(1) { [0]=> int(10) }
array(1) { [0]=> string(6) "String" }
array(1) { [0]=> bool(true) }

The examples demonstrate how to perform type casting in PHP for various data types such as boolean, integer, float, string, and array. This is useful for controlling data flow and ensuring values are in the desired format for processing.

Leave a Comment