mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-26 15:13:56 +00:00
Fix Functions Tabbing
This commit is contained in:
parent
c2b68d138c
commit
05a164fbf8
@ -379,7 +379,7 @@ for ($i = 0; $i < 5; $i++) {
|
|||||||
|
|
||||||
// Define a function with "function":
|
// Define a function with "function":
|
||||||
function my_function () {
|
function my_function () {
|
||||||
return 'Hello';
|
return 'Hello';
|
||||||
}
|
}
|
||||||
|
|
||||||
echo my_function(); // => "Hello"
|
echo my_function(); // => "Hello"
|
||||||
@ -388,8 +388,8 @@ echo my_function(); // => "Hello"
|
|||||||
// number of letters, numbers, or underscores.
|
// number of letters, numbers, or underscores.
|
||||||
|
|
||||||
function add ($x, $y = 1) { // $y is optional and defaults to 1
|
function add ($x, $y = 1) { // $y is optional and defaults to 1
|
||||||
$result = $x + $y;
|
$result = $x + $y;
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
echo add(4); // => 5
|
echo add(4); // => 5
|
||||||
@ -400,21 +400,21 @@ echo add(4, 2); // => 6
|
|||||||
|
|
||||||
// Since PHP 5.3 you can declare anonymous functions;
|
// Since PHP 5.3 you can declare anonymous functions;
|
||||||
$inc = function ($x) {
|
$inc = function ($x) {
|
||||||
return $x + 1;
|
return $x + 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
echo $inc(2); // => 3
|
echo $inc(2); // => 3
|
||||||
|
|
||||||
function foo ($x, $y, $z) {
|
function foo ($x, $y, $z) {
|
||||||
echo "$x - $y - $z";
|
echo "$x - $y - $z";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions can return functions
|
// Functions can return functions
|
||||||
function bar ($x, $y) {
|
function bar ($x, $y) {
|
||||||
// Use 'use' to bring in outside variables
|
// Use 'use' to bring in outside variables
|
||||||
return function ($z) use ($x, $y) {
|
return function ($z) use ($x, $y) {
|
||||||
foo($x, $y, $z);
|
foo($x, $y, $z);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
$bar = bar('A', 'B');
|
$bar = bar('A', 'B');
|
||||||
@ -429,14 +429,14 @@ echo $function_name(1, 2); // => 3
|
|||||||
|
|
||||||
// You can get the all the parameters passed to a function
|
// You can get the all the parameters passed to a function
|
||||||
function parameters() {
|
function parameters() {
|
||||||
$numargs = func_num_args();
|
$numargs = func_num_args();
|
||||||
if ($numargs > 0) {
|
if ($numargs > 0) {
|
||||||
echo func_get_arg(0) . ' | ';
|
echo func_get_arg(0) . ' | ';
|
||||||
}
|
}
|
||||||
$args_array = func_get_args();
|
$args_array = func_get_args();
|
||||||
foreach ($args_array as $key => $arg) {
|
foreach ($args_array as $key => $arg) {
|
||||||
echo $key . ' - ' . $arg . ' | ';
|
echo $key . ' - ' . $arg . ' | ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |
|
parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |
|
||||||
|
Loading…
Reference in New Issue
Block a user