Php get all args

PHP get all arguments as array?

Solution 1: From this answer on Server Fault: Use the binary instead of just , and pass the arguments on the command line, like this: Which puts this in : You can also set environment variables that would be set by the web server, like this: Solution 2: Typically, for passing arguments to a command line script, you will use either the global variable or getopt: refers to the HTTP GET method parameters, which are unavailable on the command line, since they require a web server to populate. ‘Requires’ statements will use the location of your command to resolve relative URLs, not the location of the file.

PHP get all arguments as array?

Hey, I was working with a PHP function that takes multiple arguments and formats them. Currently, I’m working with something like this:

function foo($a1 = null, $a2 = null, $a3 = null, $a4 = null)

But I was wondering if I can use a solution like this:

But still invoke the function the same way, similar to the params keyword in C# or the Arguments Array in JavaScript .

func_get_args returns an array with all arguments of the current function.

If you use PHP 5.6+, you can now do this:

 return $acc; > echo sum(1, 2, 3, 4); ?> 

Or as of PHP 7.1 you are now able to use a type hint called iterable

Читайте также:  Cannot modify header information headers already sent by wp login php

Also, it can be used instead of Traversable when you iterate using an interface. As well as it can be used as a generator that yields the parameters.

Php — getting function’s argument names, But it looks like you also changed your answer to reflect func_get_args() instead. I don’t believe that would give variable-named keys like you indicate there, just …

How to get PUT/DELETE arguments in PHP

i am writing a REST webservice and would like to know how can i handle put or delete argument is PHP.

i am taking the inputs as following,

$input = file_get_contents('php://input'); echo $input; 

how can i access these variables like

echo $_POST['imei']; echo $_POST['email']; echo $_GET['imei']; echo $_GET['email']; 

i found there is nothing like $_PUT or $_DELETE in php to handle the input params. what is the way to get this ?

Can you try this, PHP doesn’t have a built-in way to do this, can be read from the incoming stream to PHP, php://input .

 parse_str(file_get_contents("php://input")); 
 if($_SERVER['REQUEST_METHOD'] == 'GET') < echo "this is a get request\n"; echo $_GET['fruit']." is the fruit\n"; echo "I want ".$_GET['quantity']." of them\n\n"; >elseif($_SERVER['REQUEST_METHOD'] == 'PUT') < echo "this is a put request\n"; parse_str(file_get_contents("php://input"),$post_vars); echo $post_vars['fruit']." is the fruit\n"; echo "I want ".$post_vars['quantity']." of them\n\n"; >

Unfortunately php does not handle put or delete requests by default. I have created a library in order to handle this kind of requests. It also handles multipart requests (PUT PATCH DELETE etc)

You can find it here https://github.com/notihnio/php- request-parser

PHP func_get_arg() Function, PHP Warning: func_get_arg(): Called from the global scope — no function context in /home/main.php on line 9 For versions before PHP 5.3: …

Parsing GET request parameters in a URL that contains another URL

http://localhost/test.php?id=http://google.com/?var=234&key=234 

And I can’t get the full $_GET[‘id’] or $_REQUEST[‘d’].

$get_url = "http://google.com/?var=234&key=234"; $my_url = "http://localhost/test.php?id=" . urlencode($get_url); 
http://localhost/test.php?id=http%3A%2F%2Fgoogle.com%2F%3Fvar%3D234%26key%3D234 

So now you can get this value using $_GET[‘id’] or $_REQUEST[‘id’] (decoded).

http://google.com/?var=234&key=234 

To get every GET parameter:

foreach ($_GET as $key=>$value) < echo "$key = " . urldecode($value) . "
\n"; >

$key is GET key and $value is GET value for $key .

Or you can use alternative solution to get array of GET params

$get_parameters = array(); if (isset($_SERVER['QUERY_STRING'])) < $pairs = explode('&', $_SERVER['QUERY_STRING']); foreach($pairs as $pair) < $part = explode('=', $pair); $get_parameters[$part[0]] = sizeof($part)>1 ? urldecode($part[1]) : ""; > > 

$get_parameters is same as url decoded $_GET .

While creating url encode them with urlencode

and while fetching decode it wiht urldecode

You may have to use urlencode on the string ‘http://google.com/?var=234&key=234’

I had a similar problem and ended up using parse_url and parse_str , which as long as the URL in the parameter is correctly url encoded (which it definitely should) allows you to access both all the parameters of the actual URL, as well as the parameters of the encoded URL in the query parameter, like so:

$get_url = "http://google.com/?var=234&key=234"; $my_url = "http://localhost/test.php?id=" . urlencode($get_url); function so_5645412_url_params($url) < $url_comps = parse_url($url); $query = $url_comps['query']; $args = array(); parse_str($query, $args); return $args; >$my_url_args = so_5645412_url_params($my_url); // Array ( [id] => http://google.com/?var=234&key=234 ) $get_url_args = so_5645412_url_params($my_url_args['id']); // Array ( [var] => 234, Php get all args => 234 ) 

Php — Get URL query string parameters, Also if you are looking for current file name along with the query string, you will just need following . basename($_SERVER[‘REQUEST_URI’]) It …

PHP passing $_GET in the Linux command prompt

Say we usually it access via

http://localhost/index.php?a=1&b=2&c=3 

How do we execute the same on a Linux command prompt?

But what about passing the $ _GET variables ? Maybe something like php -e index.php —a 1 —b 2 —c 3 ? I doubt that’ll work.

From this answer on Server Fault:

Use the php-cgi binary instead of just php , and pass the arguments on the command line, like this:

php-cgi -f index.php left=1058 right=1067 language=English 
Array ( [left] => 1058 [right] => 1067 [class] => A [language] => English ) 

You can also set environment variables that would be set by the web server, like this:

REQUEST_URI='/index.php' SCRIPT_NAME='/index.php' php-cgi -f index.php left=1058 right=1067 language=English 

Typically, for passing arguments to a command line script, you will use either the argv global variable or getopt:

// Bash command: // php -e myscript.php hello echo $argv[1]; // Prints "hello" // Bash command: // php -e myscript.php -f=world $opts = getopt('f:'); echo $opts['f']; // Prints "world" 

$_GET refers to the HTTP GET method parameters, which are unavailable on the command line, since they require a web server to populate.

If you really want to populate $_GET anyway, you can do this:

// Bash command: // export QUERY_STRING="var=value&arg=value" ; php -e myscript.php parse_str($_SERVER['QUERY_STRING'], $_GET); print_r($_GET); /* Outputs: Array( [var] => value [arg] => value ) */ 

You can also execute a given script, populate $_GET from the command line, without having to modify said script:

export QUERY_STRING="var=value&arg=value" ; \ php -e -r 'parse_str($_SERVER["QUERY_STRING"], $_GET); include "index.php";' 

Note that you can do the same with $_POST and $_COOKIE as well.

Sometimes you don’t have the option of editing the PHP file to set $_GET to the parameters passed in, and sometimes you can’t or don’t want to install php-cgi .

I found this to be the best solution for that case:

php -r '$_GET["key"]="value"; require_once("script.php");' 

This avoids altering your PHP file and lets you use the plain php command. If you have php-cgi installed, by all means use that, but this is the next best thing. I thought this options was worthy of mention.

The -r means run the PHP code in the string following. You set the $_GET value manually there, and then reference the file you want to run.

It’s worth noting you should run this in the right folder, often, but not always, the folder the PHP file is in. ‘ Requires’ statements will use the location of your command to resolve relative URLs, not the location of the file.

I don’t have a php-cgi binary on Ubuntu, so I did this:

% alias php-cgi="php -r '"'parse_str(implode("&", array_slice($argv, 2)), $_GET); include($argv[1]);'"' --" % php-cgi test1.php foo=123 You set foo to 123. %cat test1.php You set foo to . 

PHP check if url parameter exists, I know this is an old question, but since php7.0 you can use the null coalescing operator (another resource).. It similar to the ternary operator, but will behave like …

Источник

PHP get all arguments as array?

Sometimes, we might need to pass an unknown number of arguments to a PHP function. So, we might wonder how to get all the arguments passed to a PHP function as an array. In this guide, we will walk you through how to achieve this in PHP.

Method 1: Using func_get_args() function

PHP provides a built-in function called `func_get_args()` which returns an array of all arguments passed to a function. Here is an example:

function getAllArguments() < $args = func_get_args(); return $args; >print_r(getAllArguments('apple', 'banana', 'cherry', 'dates')); 
Array ( [0] => apple [1] => banana [2] => cherry [3] => dates ) 

This method works well, but it has a downside. It can only be used within the function, and we cannot access it outside the function.

Method 2: Using … (Spread Operator)

PHP 5.6 introduced the spread operator, which allows us to pass an array of arguments to a function. Here is an example:

function getAllArguments(. $args) < return $args; >print_r(getAllArguments('apple', 'banana', 'cherry', 'dates')); 
Array ( [0] => apple [1] => banana [2] => cherry [3] => dates ) 

This method is more flexible, and we can use it anywhere in the code.

Conclusion

In this guide, we have shown you two methods to get all the arguments passed to a PHP function as an array. The first method uses the `func_get_args()` function, and the second method uses the spread operator. Both methods are useful, and you should use the one that suits your needs.

Источник

Php get all args

To experiment on performance of pass-by-reference and pass-by-value, I used this script. Conclusions are below.

#!/usr/bin/php
function sum ( $array , $max ) < //For Reference, use: "&$array"
$sum = 0 ;
for ( $i = 0 ; $i < 2 ; $i ++)#$array[$i]++; //Uncomment this line to modify the array within the function.
$sum += $array [ $i ];
>
return ( $sum );
>

$max = 1E7 //10 M data points.
$data = range ( 0 , $max , 1 );

$start = microtime ( true );
for ( $x = 0 ; $x < 100 ; $x ++)$sum = sum ( $data , $max );
>
$end = microtime ( true );
echo «Time: » .( $end — $start ). » s\n» ;

/* Run times:
# PASS BY MODIFIED? Time
— ——- ——— —-
1 value no 56 us
2 reference no 58 us

3 valuue yes 129 s
4 reference yes 66 us

1. PHP is already smart about zero-copy / copy-on-write. A function call does NOT copy the data unless it needs to; the data is
only copied on write. That’s why #1 and #2 take similar times, whereas #3 takes 2 million times longer than #4.
[You never need to use &$array to ask the compiler to do a zero-copy optimisation; it can work that out for itself.]

2. You do use &$array to tell the compiler «it is OK for the function to over-write my argument in place, I don’t need the original
any more.» This can make a huge difference to performance when we have large amounts of memory to copy.
(This is the only way it is done in C, arrays are always passed as pointers)

3. The other use of & is as a way to specify where data should be *returned*. (e.g. as used by exec() ).
(This is a C-like way of passing pointers for outputs, whereas PHP functions normally return complex types, or multiple answers
in an array)

5. Sometimes, pass by reference could be at the choice of the caller, NOT the function definitition. PHP doesn’t allow it, but it
would be meaningful for the caller to decide to pass data in as a reference. i.e. «I’m done with the variable, it’s OK to stomp
on it in memory».
*/
?>

Источник

Оцените статью