You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/anonymous-functions-closures/about.md
+2-3Lines changed: 2 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,8 +26,7 @@ $add5 = makeAdderFunction(5);
26
26
echo $add5(4); // echos 9
27
27
```
28
28
29
-
In the function `makeAdderFunction` above, the `$number` value that is passed in is "closed" over withing the internally defined anonymous function which is then returned. Then whenever that function is executed it knows the value of the `$number` that was passed in. If you were to call this function again with another number, that would return a completely different function. It would not change the values or the functionality returned by the first function call.
30
-
29
+
In the function `makeAdderFunction` above, the `$number` value that is passed in is "closed over" within the internally defined anonymous function which is then returned. Then whenever that function is executed it knows the value of the `$number` that was passed in. If you were to call this function again with another number, that would return a completely different function. It would not change the values or the functionality returned by the first function call.
31
30
If you need to close over more than one variable, they can be added into the `use` keyword separated by a comma.
32
31
33
32
PHP also has a shorter function syntax called `arrow functions`. They are required to be fairly simple. The basic form of these is:
@@ -63,7 +62,7 @@ function positiveAverage(array $numbers): float
The division operator gives the result of dividing the left value by the right value. If the right value is 0, it will cause a `DivisionByZeroError` error.
48
+
The division operator gives the result of dividing the left value by the right value. If the right value is 0, it will cause a `DivisionByZeroError` error.
49
49
50
50
```php
51
51
$slicesOfPizza = 8;
@@ -79,7 +79,7 @@ if ($isOdd === 1) {
79
79
80
80
## Exponentiation (**)
81
81
82
-
The exponentiation operator is used to raise the left value to the power of the right value. Prior to PHP 5.6 you would need to use the `pow` function to perform the same thing.
82
+
The exponentiation operator is used to raise the left value to the power of the right value. Prior to PHP 5.6 you would need to use the `pow` function to perform the same thing.
PHP follows the standard order of operations for math. This means that rather that performing operations left to right, each operator has a precedence and the operations are resolved according to their precedence. The order of oeprations is:
98
+
PHP follows the standard order of operations for math. This means that rather that performing operations left to right, each operator has a precedence and the operations are resolved according to their precedence. The order of operations is:
99
99
100
100
* parentheses
101
101
* exponentiation
102
102
* multiplication, division
103
103
* addition, subtraction
104
104
105
-
Multiplication and division are at the same level of precedence as each other. Similarly, addition and subtraction are at the same level of precence.
105
+
Multiplication and division are at the same level of precedence as each other. Similarly, addition and subtraction are at the same level of precedence.
106
106
107
107
This means the value below would be 17 instead of 10, since multiplication has a higher precedence than addition:
108
108
109
109
```php
110
110
$value = 2 + 3 * 5;
111
111
```
112
112
113
-
PHP also provides a number of helpful assignment operators that combine arithmetic operators with assigment when you are perfoming an operation on a variable and assigning the result back to itself. These will be covered in the `Assignment Operators` concept.
113
+
PHP also provides a number of helpful assignment operators that combine arithmetic operators with assignment when you are performing an operation on a variable and assigning the result back to itself. These will be covered in the `Assignment Operators` concept.
0 commit comments