Saturday, July 16, 2005

PHP note: Difference between PHP and Python - scope

Scope in Python is more complex. Firstly, functions. You will encounter local, global and builtin scopes in a function. Local scope is the scope within functions, global scope is the currently running module scope, while builtin scope is the current running python program. Secondly, objects. You will encounter object, class scope. Python searches for variables in sequence of scopes, and looks at a higer level scope when it can't find symbol in the current scope.

Scope in PHP is just simple. Normally you only need a module scope, while in functions there is a local scope. PHP does not search super scopes though. This is somehow because PHP can always find symbols. When variable not defined, PHP will simply set an empty value to it.

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.
Haloscan commenting and trackback have been added to this blog.