Update page 'P# 1.0 Draft Specification'
@@ -217,7 +217,7 @@ Array is a complex data type which handles a collection of elements. Each of the
|
||||
|
||||
Above example will create a multi-dimensional associative array containing integers. The output of var_dump() function should return the following output:
|
||||
|
||||
array(10) {
|
||||
array(9) {
|
||||
[0] => int(1),
|
||||
[1] => int(2),
|
||||
[2] => int(3),
|
||||
@@ -226,8 +226,10 @@ Above example will create a multi-dimensional associative array containing integ
|
||||
[5] => int(6),
|
||||
[6] => int(7),
|
||||
["test"] => int(8),
|
||||
[7] => int(0),
|
||||
[8] => int(0),
|
||||
[7] => array(2) {
|
||||
[0] =>int(0),
|
||||
[1] => int(0),
|
||||
}
|
||||
}
|
||||
|
||||
### 5.13. Type Casting
|
||||
@@ -247,7 +249,99 @@ An example:
|
||||
%>
|
||||
|
||||
## 6. Operators
|
||||
TODO
|
||||
Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. There are three kinds of operators:
|
||||
* Unary operators - the unary operators take one operand and use either prefix notation (such as –$x) or
|
||||
postfix notation (such as $x++),
|
||||
* Binary operators - the binary operators take two operands and all use infix notation (such as $x + $y),
|
||||
* Ternary operator. Only one ternary operator, ?:, exists; it takes three operands and uses infix notation
|
||||
(c ? x : y).
|
||||
The order of evaluation of operators in an expression is determined by the precedence of the operators.
|
||||
|
||||
### 6.1. Operator Precedence
|
||||
When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. The precedence of an operator is established by the definition of its associated grammar production. The following list shows common PHP operators ordered by precedence (highest precedence first):
|
||||
* increment/decrement (++ --)
|
||||
* casting
|
||||
* logical "not" (!)
|
||||
* bitwise "not" (~)
|
||||
* arithmetic (* / %)
|
||||
* arithmetic and string (+ -)
|
||||
* bitwise (<< >>)
|
||||
* comparison (< <= > >= <>)
|
||||
* comparison (== != === !==)
|
||||
* bitwise "and" (&)
|
||||
* bitwise "xor" (^)
|
||||
* bitwise "or" (|)
|
||||
* logical "and" (&&)
|
||||
* logical "xor" (^^)
|
||||
* logical "or" (||)
|
||||
* ternary operator (? :)
|
||||
* assignment (= += -= *= /= .= %=)
|
||||
* comma operator (,)
|
||||
|
||||
### 6.2. Associativity Rule
|
||||
Sometimes the precedence is not satisfactory to determine the outcome of an expression. There is another rule called associativity. The associativity of operators determines the order of evaluation of operators with the same precedence level.
|
||||
|
||||
9 / 3 * 3
|
||||
|
||||
What is the outcome of this expression, 9 or 1? The multiplication, deletion, and the modulo operator are left to right associated. So the expression is evaluated this way: (9 / 3) * 3 and the result is 9. Arithmetic, boolean, relational, and bitwise operators are all left to right associated. On the other hand, the assignment operator is right associated.
|
||||
|
||||
### 6.3. Assignment Operator
|
||||
The assignment operator = assigns a value to a variable. A variable is a placeholder for a value. In P#, like in PHP or Perl, a variable begins with a $ character.
|
||||
|
||||
<%
|
||||
$x = 1;
|
||||
%>
|
||||
|
||||
### 6.4. Arithmetic Operators
|
||||
P# offers the following arithmetic operators:
|
||||
* Addition: +
|
||||
* Subtraction: -
|
||||
* Multiplication: *
|
||||
* Division: /
|
||||
* Modulo: %
|
||||
|
||||
### 6.5. Concatenating Strings
|
||||
In P# concatenating strings is nothing more than adding strings to each other with the addition operator.
|
||||
|
||||
<%
|
||||
string $str = "Hello" + ' ' + 'World!';
|
||||
%>
|
||||
|
||||
### 6.6. Boolean Operators
|
||||
In P#, there are and (&&), or (||), xor (^^) and negation (!) boolean operators. With boolean operators logical operations are performed. These are often used with if and while keywords.
|
||||
|
||||
### 6.7. Relational Operators
|
||||
Relational operators are used to compare values. These operators always result in boolean value. P# implements the following ones:
|
||||
* strictly less than: <
|
||||
* less than or equal to: <=
|
||||
* strictly greater than: >
|
||||
* greater than or equal to: >=
|
||||
* equal to: ==
|
||||
* not equal to: !=
|
||||
* identical: ===
|
||||
* not identical: !==
|
||||
|
||||
### 6.8. Bitwise Operators
|
||||
Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal or hexadecimal symbols are only notations of a number. Bitwise operators work with bits of a binary number. P# also have binary logical operators and shift operators:
|
||||
* bitwise negation: ~
|
||||
* bitwise exclusive or (xor): ^
|
||||
* bitwise and: &
|
||||
* bitwise or: |
|
||||
* left shift: <<
|
||||
* right shift: >>
|
||||
|
||||
### 6.9. Compound Assignment Operators
|
||||
The compound assignment operators consist of two operators. They are shorthand operators. The ccompound operators are: += -= *= /= %= &= |= ^= >>= <<=
|
||||
|
||||
### 6.10. Ternary Operator
|
||||
Unlike the standard PHP engine, the ternary operator under P# evaluates left-to-right, associates right-to-left. That is, there is no need for parenthesis for nested ternary operators.
|
||||
|
||||
<%
|
||||
string $var = true ? 'true' : false? 't' : 'f';
|
||||
print($var);
|
||||
%>
|
||||
|
||||
The above expression in PHP will output 't' and this is an unexpected result, but if the same code run using the P# will output 'true' as expected.
|
||||
|
||||
## 7. Flow Control
|
||||
TODO
|
||||
|
||||
Reference in New Issue
Block a user