Supported types include:
- String
- Integer
- Float
- Boolean
- Array
- Object
As well as null
.
For numbers, Python will dynamically change types internally as needed (for specificity, length, etc.). However, you can manually force Python to use a specific number type if needed. Number types include int
, long
, and complex
, and types can be manually set by use of their respective methods, int()
, float()
, and complex()
.
Other supported types include:
- String
- List (array)
- Tuple
- Dictionary
- Object
As well as None
.
By default, PHP will pass types by value.
However, PHP allows for variables and references returned from functions to be passed and referred to by reference by prefacing variables with &
.
<?php
function foo(&$bar) {
$bar++;
}
$baz = 5;
foo();
print($baz);
?>
Will yield 6
.
Python has no explicit rules for declaring how variable types are passed. If an object, tuple, array, etc. is passed in Python the object's reference will be passed rather than its value.
def foo(bar):
bar.append(3)
baz = [0, 1, 2]
>>>print(baz)
[0, 1, 2]
foo(baz)
>>>print(baz)
[0, 1, 2, 3]
However when passing strings or numbers, Python will behave as if they are passed by value. For example, when passing a number:
def foo(bar):
bar+=1
print("Bar from inside foo: %d"%bar)
baz = 4
>>>print(baz)
4
>>>foo(baz)
Bar from inside foo: 5
>>>print(baz)
4
PHP does not allow for creation of new types. The closest you can get to this behavior would be to define a new class, but PHP treats all defined classes generically as class objects.
Not natively. Python allows for extensions and has documentation for defining new types, but this requires a separate compilation step and programming in C, the functionality is not native to in-language tools.