From c2b68d138cb881994836040fb10e220f0d4fcc93 Mon Sep 17 00:00:00 2001 From: Akshay Kalose Date: Fri, 16 Oct 2015 20:36:19 -0400 Subject: Add Getting Parameters From Functions in PHP --- php.html.markdown | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 39ec5aef..9d9c5847 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -426,6 +426,21 @@ echo $function_name(1, 2); // => 3 // Useful for programatically determining which function to run. // Or, use call_user_func(callable $callback [, $parameter [, ... ]]); + +// You can get the all the parameters passed to a function +function parameters() { + $numargs = func_num_args(); + if ($numargs > 0) { + echo func_get_arg(0) . ' | '; + } + $args_array = func_get_args(); + foreach ($args_array as $key => $arg) { + echo $key . ' - ' . $arg . ' | '; + } +} + +parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World | + /******************************** * Includes */ -- cgit v1.2.3 From 05a164fbf8c752071866b5816967dbd1d2a2ee30 Mon Sep 17 00:00:00 2001 From: Akshay Kalose Date: Fri, 16 Oct 2015 21:43:47 -0400 Subject: Fix Functions Tabbing --- php.html.markdown | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 9d9c5847..de20f4c9 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -379,7 +379,7 @@ for ($i = 0; $i < 5; $i++) { // Define a function with "function": function my_function () { - return 'Hello'; + return 'Hello'; } echo my_function(); // => "Hello" @@ -388,8 +388,8 @@ echo my_function(); // => "Hello" // number of letters, numbers, or underscores. function add ($x, $y = 1) { // $y is optional and defaults to 1 - $result = $x + $y; - return $result; + $result = $x + $y; + return $result; } echo add(4); // => 5 @@ -400,21 +400,21 @@ echo add(4, 2); // => 6 // Since PHP 5.3 you can declare anonymous functions; $inc = function ($x) { - return $x + 1; + return $x + 1; }; echo $inc(2); // => 3 function foo ($x, $y, $z) { - echo "$x - $y - $z"; + echo "$x - $y - $z"; } // Functions can return functions function bar ($x, $y) { - // Use 'use' to bring in outside variables - return function ($z) use ($x, $y) { - foo($x, $y, $z); - }; + // Use 'use' to bring in outside variables + return function ($z) use ($x, $y) { + foo($x, $y, $z); + }; } $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 function parameters() { - $numargs = func_num_args(); - if ($numargs > 0) { - echo func_get_arg(0) . ' | '; - } - $args_array = func_get_args(); - foreach ($args_array as $key => $arg) { - echo $key . ' - ' . $arg . ' | '; - } + $numargs = func_num_args(); + if ($numargs > 0) { + echo func_get_arg(0) . ' | '; + } + $args_array = func_get_args(); + foreach ($args_array as $key => $arg) { + echo $key . ' - ' . $arg . ' | '; + } } parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World | -- cgit v1.2.3