Saturday, July 09, 2005

PHP note: Difference between PHP and Python - types

PHP and Python are both interpreted languages.

Python has more strict types from PHP. Python does not have implicit type casting. For example, Python will calculate 3/2 as an integer division, and the output could be 1. The only implicit type casting that I find about Python is that Python will change integers to long integers when the value of that integer is too large.

PHP, on the other hand, does implicit type casting all the time. It does not have integer division at all. When it does 3/2 it simply yields the float result 1.5. What is more unacceptable from Python thoughts is that it turns strings to integers automatically. "15abc" + 10 = 25! The same understanding of the world applies to many other places in PHP. Such as when you meet 01081 it will be taken as 8. Because that was an octal number, which was truncated when 8 came up.


mangaiappan said...

Hi friend,
Did you mean 3/2 yields 1 in python and yields 1.5 in php or your blog refers to 3/1 does it ......

Yue Zhang said...

Thanks, mangaiappan. Typo fixed.