-
Notifications
You must be signed in to change notification settings - Fork 14
Differences Between PHP and PHP+QB
PHP+QB functions are typically compiled first as PHP functions (imported Pixel Bender kernels are the sole exception). Despite having the exact same syntax, PHP+QB really should be considered as its own separate programming language, however. Semantically, PHP+QB can differ quite substantially from regular PHP. The same expression can be one thing in the former and something else entirely in the latter:
There is strict typing in PHP+QB. Variables will never go from one type to another. In particular, integers are not automatically promoted to floating point when the value it holds exceeds the maximum. When an integer overflow occurs, the variable wraps around to its minimum possible value. For example, if $a has the value 255 and was declared as uint8, an increment operation would change the value to 0.
Integer can be signed or unsigned in PHP+QB. The signedness of a variable changes the behavior of the bitwise shift operators. Arithmetic shift is performed on signed integers (like in PHP). Logical shift is performed on unsigned integers.
String is not a primitive type in PHP+QB. When a string is passed to a PHP+QB function, it will be interpreted as a blob of one of the primitive types. The keyword “string” in QB type declaration means “an expandable array of uint8.” The expression $s[1] yields the ASCII value of the second character of $s. For example, if $s is “100”, the value of $s[1] is 48 (the codepoint of zero). In PHP, you would get 0 instead due to automatic type conversion.
Arrays are simply blocks of memory in PHP+QB. There are no associative arrays. Indices into arrays must always be positive integers (or zero). The action performed operators on arrays in PHP+QB are quite different from what they do in PHP. If $a and $b are arrays, $a + $b means component-wise addition of $a and $b (the first element of the result is $a[0] + $a[0], the second is $a[1] + $a[1], and so forth). If $a contains [1, 2, 3, 4] and $b contains [5, 6, 7, 8], then $a + $b yields [6, 8, 10, 12]. In PHP, $a + $b means a union of $a and $b, so the result is [1, 2, 3, 4].
Multiplication of two arrays is possible in PHP+QB, as is division, modulo, and subtraction.
In PHP+QB, division of one integer by another integer produces an integer. The expression 7 / 8 gives 0, for instance. In PHP, the same operation yields 0.875.
In PHP+QB, division by zero triggers an error only if the operands are integers. For floating point numbers, the result is either -INF or INF.
The % operator can be used on floating point numbers in PHP+QB. In PHP, you would need to call fmod().
Reference can only be used when parameters are passed to PHP+QB functions. It's not possible to have two names pointing to the same variable, nor is it possible to create a reference to an array element.
There are no objects in PHP+QB. When object methods are compiled for PHP+QB, they are allowed to access properties of $this, however.
There is no exception handling in PHP+QB.
In general, the design goal of QB is to follow the semantic of PHP as closely as possible. Where divergence occurs, it is always due to either strict typing or difference in array representation.