PHP Interview questions

Total available count: 33
Subject - PHP Frameworks
Subsubject - PHP

What is Type Juggling and Type casting?

PHP does not need (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, it becomes a string. If an integer value is assigned to $var, it becomes an integer.

Example: PHP's automatic type conversion is the multiplication operator '*'. If 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 itselfs; the only change is in how the operands are evaluated and what the type of the expression itself is.

$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 * "20 Little Cats"; // $foo is integer (50)
$foo = 5 * "20 Small Cats"; // $foo is integer (50)


Next 5 interview question(s)

What is the difference between FUNCTION and METHOD?
What are Magic constants?
How to define constants?
What is need for using static variables?
What is the difference between global vs $GLOBALS?