Explain about Type Juggling in PHP?

Answers were Sorted based on User's Feedback



Explain about Type Juggling in PHP?..

Answer / rekha_sri

In PHP there is no need to declare the variable with
explicit type declaration. Variable type is specify by the
context.

For example, if we assign the integer value into variable,it
will be integer variable and if we assign the string value
into variable it will be string.

Example:
--------
<?php
$a=10;
$b="string";
echo "$a"; // 10
echo "$b"; // String
?>

If we want to force the variable type into different type we
can do the type casting.

Type casting example:
---------------------
<?php
$one = 10; // $one is an integer
$two = (boolean) $one; // $two is a boolean
?>

In PHP we are having function called settype() for setting
the particular variable type.

settype() example:
------------------
<?php
$first = "10rose"; // string
$second = true; // boolean

settype($first, "integer"); // $first is now 10 (integer)
settype($second, "string"); // $second is now "1" (string)
?>

Is This Answer Correct ?    8 Yes 0 No

Explain about Type Juggling in PHP?..

Answer / rakesh kumar nautiyal

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

An example of PHP's automatic type conversion is the
addition operator '+'. If any of the operands is a float,
then all 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.


<?php
$foo = "0"; // $foo is string (ASCII 48)
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
?>



If the last two examples above seem odd, see String
conversion to numbers.

If you wish to force a variable to be evaluated as a
certain type, see the section on Type casting. If you wish
to change the type of a variable, see settype().

If you would like to test any of the examples in this
section, you can use the var_dump() function.

Note: The behaviour of an automatic conversion to array is
currently undefined.



<?php
$a = "1"; // $a is a string
$a[0] = "f"; // What about string offsets? What happens?
?>



Since PHP (for historical reasons) supports indexing into
strings via offsets using the same syntax as array
indexing, the example above leads to a problem: should $a
become an array with its first element being "f", or
should "f" become the first character of the string $a?

The current versions of PHP interpret the second assignment
as a string offset identification, so $a becomes "f", the
result of this automatic conversion however should be
considered undefined. PHP 4 introduced the new curly
bracket syntax to access characters in string, use this
syntax instead of the one presented above:
<?php
$a = "abc"; // $a is a string
$a{1} = "f"; // $a is now "afc"
?>

Is This Answer Correct ?    4 Yes 3 No

Post New Answer

More PHP Interview Questions

Is php a float?

0 Answers  


Is wordpress a php framework?

0 Answers  


What are the uses of explode() and implode() functions?

0 Answers  


Tell me what is the use of "enctype" attribute in a html form?

0 Answers  


Why use php artisan serve?

0 Answers  






What is the delimiter default in PHP?

0 Answers  


Do you know how can we check the value of a given variable is a number?

0 Answers  


How to set session.gc_divisor properly?

0 Answers  


What are the differences between echo and print?

0 Answers  


Difference between notify url, return url in paypal payment gateway?

4 Answers   A1 Technology, Capital IQ, CMS, Essar, HCL, IAS, Karvy, L&T, State Bank Of India SBI, TATA, Tisco, WNS,


What is encapsulation in oop php?

0 Answers  


What is the difference between ereg_replace() and eregi_replace()?

2 Answers   Quicknet,


Categories