PHP type casting

PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used.

PHP is supper-friendly. Also, it is a loosely typed language. This means PHP allows us to declare a variable and simply use it, the data type will be automatically determined.

Note: PHP determines the data type of variables automatically. We just declare a variable and assign it a value. PHP determines the type from the value.

Furthermore, PHP continuously and automatically converts types from one to another when necessary. This is called implicit casting or Type Juggling. Why is PHP juggling types? Because it is always trying to make sense of our code.

An example of PHP's automatic type conversion is the multiplication operator '*'. If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as ints, and the result will also be an int. Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated and what the type of the expression itself is.

<?php

$foo = "1";  // $foo is string (ASCII 49)

$foo *= 2;   // $foo is now an integer (2)

$foo = $foo * 1.3;  // $foo is now a float (2.6)

$foo = 5 * "10 Little Piggies"; // $foo is integer (50)

$foo = 5 * "10 Small Pigs";     // $foo is integer (50)

?>

Type Casting 

Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.

<?php

$foo = 10;   // $foo is an integer

$bar = (boolean) $foo;   // $bar is a boolean

?>

The casts allowed are

(int), (integer) - cast to int

(bool), (boolean) - cast to bool

(float), (double), (real) - cast to float

(string) - cast to string

(array) - cast to array

(object) - cast to object

(unset) - cast to NULL

(binary) casting and b prefix exists for forward support. Note that the (binary) cast is essential the same as (string), but it should not be relied upon.

The (unset) cast has been deprecated as of PHP 7.2.0. Note that the (unset) cast is the same as assigning the value NULL to the variable or call. The (unset) cast is removed as of PHP 8.0.0.

"An example of PHP's automatic type conversion is the multiplication operator '*'. If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated and what the type of the expression itself is."

I understand what the doc is trying to say here, but this sentence is not correct as stated, other types can be coerced into floats.

e.g.

<?php

$a = "1.5"; // $a is a string

$b = 100; // $b is an int

$c = $a * $b; // $c is a float, value is 150

// multiplication resulted in a float despite fact that neither operand was a float

?>

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...