How to get value from command line in php (2 values)
I run it in ubuntu, wish i'm new here.
I am trying to add 2 mixed numbers (integer with fractions)
php fractions.php "1 1/7" "2 2/7"
how to get these 2 values ββand make them a variable from command line? then I will also output the output to the command line thanks in advance.
+3
source to share
1 answer
there are a few things to help and make things tidy
edit your php.ini file, find register_argc_argv and make sure it is set to:
register_argc_argv = On
create your fraction.php script for example:
#!/usr/bin/php -q
<?php
echo "arg1: ".$argv[1]."\n";
echo "arg2: ".$argv[2]."\n";
NOTE: #! line is a smart bit, so you don't need to use php for the command line when running the script./usr/bin/php may be different on your computer:
# whereis php
chmod script, so you can run it directly from the command line:
# chmod 755 fraction.php
Finally, run the script:
. /fraction.php "1 1/7" "2 2/7"
0
source to share