Types¶
Zephir is both dynamically and statically typed. In this chapter we highlight the supported types and their behaviors.
Dynamic Types¶
Dynamic variables are exactly like the ones in PHP. They can be assigned and reassigned to different types without restriction.
A dynamic variable must be declared with the keyword var
. The behavior is nearly the same as in PHP:
Initialize variables¶
Change their values¶
Perform operations¶
They can have eight types:
Type | Description |
---|---|
array | An array is an ordered map. A map is a type that associates values to keys. |
boolean | A boolean expresses a truth value. It can be either true or false . |
float /double | Floating point numbers. The size of a float is platform-dependent. |
integer | Integer numbers. The size of an integer is platform-dependent. |
null | The special NULL value represents a variable with no value. |
object | Object abstraction like in PHP. |
resource | A resource holds a reference to an external resource. |
string | A string is series of characters, where a character is the same as a byte. |
Check more info about these types in the PHP manual.
Array¶
The array implementation in Zephir is basically the same as in PHP: ordered maps optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
The syntax to define arrays is slightly different than in PHP:
Square braces must be used to define arrays¶
Double colon must be used to define hashes' keys¶
Only long and string values can be used as keys:
let myHash = [0: "first", 1: true, 2: null];
let myHash = ["first": 7.0, "second": "some string", "third": false];
Boolean¶
A boolean expresses a truth value. It can be either true
or false
:
Float/Double¶
Floating-point numbers (also known as "floats", "doubles", or "real numbers"). Floating-point literals are expressions with one or more digits, followed by a period (.), followed by one or more digits. The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).
Floating point numbers have limited precision. Although it depends on the system, Zephir uses the same IEEE 754 double precision format used by PHP, which will give a maximum relative error due to rounding in the order of 1.11e-16.
Integer¶
Integer numbers. The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers so Zephir has this restriction too:
Integer overflow¶
Contrary to PHP, Zephir does not automatically check for integer overflows. Like in C, if you are doing operations that may return a big number, you should use types such as unsigned long
or float
to store them:
Object¶
Zephir allows to instantiate, manipulate, call methods, read class constants, etc from PHP objects:
String¶
A string
is series of characters, where a character is the same as a byte. As PHP, Zephir only supports a 256-character set, and hence does not offer native Unicode support.
In Zephir, string literals can only be specified using double quotes (like in C or Go). Single quotes are reserved for char
data type.
The following escape sequences are supported in strings:
Sequence | Description |
---|---|
\t | Horizontal tab |
\n | Line feed |
\r | Carriage return |
\\ | Backslash |
\" | double-quote |
In Zephir, strings don't support variable parsing like in PHP; you need to use concatenation instead:
Static Types¶
Static typing allows the developer to declare and use some variable types available in C. Variables can't change their type once they're declared as static types. However, they allow the compiler to do a better optimization job. The following types are supported:
Type | Description |
---|---|
array | A structure that can be used as hash, map, dictionary, collection, stack, etc. |
boolean | A boolean expresses a truth value. It can be either true or false . |
char | Smallest addressable unit of the machine that can contain basic character set. |
float /double | Double precision floating-point type. The size is platform-dependent. |
integer | Signed integers. At least 16 bits in size. |
long | Long signed integer type. At least 32 bits in size. |
string | A string is a series of characters, where a character is the same as a byte. |
unsigned char | Same size as char , but guaranteed to be unsigned. |
unsigned integer | Unsigned integers. At least 16 bits in size. |
unsigned long | Same as long , but unsigned. |
Boolean¶
A boolean
expresses a truth value. It can be either true
or false
. Contrary to the dynamic behavior detailed above, static boolean
types remain boolean
(true
or false
) no mater what value is assigned to them:
automatically casted to true
¶
automatically casted to false
¶
throws a compiler exception¶
Char/Unsigned Char¶
char
variables are the smallest addressable unit of the machine that can contain the basic character set (generally 8 bits). A char
variable can be used to store any character in a string:
stores 't'¶
char
literals must be enclosed in single quotes¶
Integer/Unsigned Integer¶
integer
values are like the integer
member in dynamic values. Values assigned to integer variables remain integer:
automatically casted to 100¶
automatically casted to 0¶
automatically casted to 0¶
throws a compiler exception¶
unsigned integer
variables are like integer
but they don't have sign, this means you can't store negative numbers in these sort of variables:
automatically casted to 70¶
automatically casted to 100¶
automatically casted to 0¶
automatically casted to 0¶
throws a compiler exception¶
unsigned integer
variables are twice bigger than standard integer
. Assigning unsigned integer
to standard (signed) integer
may result in loss of data:
potential loss of data for b
¶
Long/Unsigned Long¶
long
variables are twice bigger than integer
variables, thus they can store bigger numbers. As with integer
, values assigned to long
variables are automatically casted to this type:
automatically casted to 100¶
automatically casted to 0¶
automatically casted to 0¶
throws a compiler exception¶
unsigned long
are like long
but they are not signed, this means you can't store negative numbers in these sort of variables:
automatically casted to 70¶
automatically casted to 100¶
automatically casted to 0¶
automatically casted to 0¶
throws a compiler exception¶
unsigned long
variables are twice bigger than standard long
; assigning unsigned long
to standard (signed) long
may result in loss of data:
potential loss of data for b
¶
String¶
A string is series of characters, where a character is the same as a byte. As in PHP it only supports a 256-character set, and hence does not offer native Unicode support.
When a variable is declared string
it never changes its type: