explain in brief type juggling in php?



explain in brief type juggling in php?..

Answer / mohammed khalid khan

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.

$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 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 behavior of an automatic conversion to array is
currently undefined.

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

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More PHP Interview Questions

What is singleton pattern in php?

0 Answers  


Explain setcookie() function in php?

0 Answers  


class Database { public static $_instance; public static function getInstance() { if(!isset(self::$_instance)) self::$_instance = new Database(DB_SERVER, DB_USER, DB_PASS, DB_NAME); print_r(self::$_instance); return self::$_instance; } } can any one explain "self::$_instance = new Database(DB_SERVER, DB_USER, DB_PASS, DB_NAME);" this line

1 Answers  


Is set in php?

0 Answers  


How many ways there are to fetch the data from the Database?

2 Answers   Tops Technologies,






Tell me how can we determine whether a php variable is an instantiated object of a certain class?

0 Answers  


What’s the difference between md5(), crc32() and sha1() crypto on PHP? -

2 Answers  


How can we display the output directly to the browser?

0 Answers  


What is an associative array?

0 Answers  


What does csrf token mean?

0 Answers  


How can you make a connection with mysql server using php?

0 Answers  


What are the different types of PHP arrays?

0 Answers  


Categories