Explain about Type Juggling in php with an example.
In php, data type of a variable need to be explicitly specified. The data type is assigned depending on the context.
Example:$x = 1;
Automatically assigns “INTEGER” data type.
The data type can be overridden by explicitaly specifying the type:
$y = (float) $x;
Settype() function can also be used.
Explain about Type Juggling in php with an example.
Ternary conditional operator is used to steer assignment statements based on the condition. Great amount of time is saved when authoring the script as well as executing it. For example, a normal conditional statement like
If(condition) ¨
Variable = value1 ;
Else
Variable = value2 ;
Begins by evaluating the condition. If the condition returns true, it assigns the value1 to the variable else assigns the value2 to the variable.
A simple and perfect solution for the above is the usage of ternary operator. The ternary operator evaluates the condition and assigns value1 if the condition returns true, assigns value2 if the condition is returns false. The above code snippet can be concatenated into a single statement, like
variable = condition?value1:value2;
It treats the whole as a single statement. It does the same what the above if .. else does.
However, a simple situation like the above is a perfect candidate to convert to a ternary conditional.