Php pass arguments to script

Php pass arguments to script

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)

Читайте также:  Pytelegrambotapi send message html

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».
*/
?>

Источник

$argv

Contains an array of all the arguments passed to the script when running from the command line.

Note: The first argument $argv[0] is always the name that was used to run the script.

Note: This variable is not available when register_argc_argv is disabled.

Examples

Example #1 $argv example

When executing the example with: php script.php arg1 arg2 arg3

The above example will output something similar to:

array(4) < [0]=>string(10) "script.php" [1]=> string(4) "arg1" [2]=> string(4) "arg2" [3]=> string(4) "arg3" >

Notes

See Also

User Contributed Notes 6 notes

Please note that, $argv and $argc need to be declared global, while trying to access within a class method.

class A
public static function b ()
var_dump ( $argv );
var_dump (isset( $argv ));
>
>

A :: b ();
?>

will output NULL bool(false) with a notice of «Undefined variable . «

whereas global $argv fixes that.

To use $_GET so you dont need to support both if it could be used from command line and from web browser.

foreach ($argv as $arg) $e=explode(» note» >

You can reinitialize the argument variables for web applications.
So if you created a command line, with some additional tweaks you can make it work on the web.

If you come from a shell scripting background, you might expect to find this topic under the heading «positional parameters».

Sometimes $argv can be null, such as when «register-argc-argv» is set to false. In some cases I’ve found the variable is populated correctly when running «php-cli» instead of just «php» from the command line (or cron).

I discovered that `register_argc_argv` is alway OFF if you use php internal webserver, even if it is set to `On` in the php.ini.

What means there seems to be no way to access argv / argc in php internal commandline server.

Источник

Php pass arguments to script

I’ve recently received some comments on a previous article I wrote about scheduling PHP scripts using Cron that were asking how to pass parameters to the script. In this article I look at a number of different methods this can be done.

Note that if register_argc_argv is disabled (set to false) in php.ini, these methods will not work.

First lets see what will 100% not work. If you’re used to passing command line parameters to a PHP script in a web browser using a query parameter (and then looking it up using $_GET), you may be trying to do something like this.

Of course PHP will promptly reject this with an error like this.

The reason that happens is because PHP is looking for a file called «script.php?param1=value1» but the script is actually called «script.php». The file system is not a web server so it doesn’t know how to handle query parameters. A different approach is required.

Lets look at the ‘classic’ method to pass command line parameters to a script — using $argc and $argv.

Here’s a PHP script that displays all of the command line arguments that were passed to it.

To pass command line arguments to the script, we simply put them right after the script name like so.

Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array. This approach works, but it is very simplistic and doesn’t play well if you’re looking to transition from a query parameter way of passing in values to your script. With this approach there is no way to give names the the command line arguments being passed in.

If you want to be able to assign variable names for the values being passed in, getopt() is the way to do it. Lets look at a different version of the script now.

Lets run this script like this.

There are some major differences here. First with getopt() you must specify which command line argument you want to retrieve. In the case of this script, it looks for the «-p» argument, that’s specified by the «p:» value passed to getopt(). The colon (:) means that the parameter must have a value. If you’re used to doing something like «script.php?p=value1» this is an equivalent for the command line.

It’s possible to pass multiple values in as well e.g.

In this case, the value changes to an array, so it’s important to check the value type before using it.

If you want to pass in multiple differently named parameters e.g. «p» and «q», you change the getopt() call like this.

Then running the script like this.

The above way of using getopt() limits your to using single character parameter names, what if you wanted to use a parameter name like «name»? This is also possible, we just change getopt() call to this.

When using long parameter names, the way that the PHP script is run changes slightly, in this case we pass the parameter like so.

This can be expanded to multiple differently named parameters too. Passing the same named parameter multiple times also results in the value being of an array type, same as described earlier. It is also possible to pass values with a space in them by putting them in quotes.

This article doesn’t cover all of the possibilities with getopt() but it gives a starting point and hopefully now you can convert your script that uses the usual query parameters with $_GET to something that uses one of the approaches outlined above.

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.

NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

Источник

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