Pass variable to php script running from command line










59















I have a PHP file that is needed to be run from command line (via crontab). I need to pass type=daily to the file but I don't know how. I tried:



php myfile.php?type=daily


but this error was returned:




Could not open input file: myfile.php?type=daily




What can I do?










share|improve this question




























    59















    I have a PHP file that is needed to be run from command line (via crontab). I need to pass type=daily to the file but I don't know how. I tried:



    php myfile.php?type=daily


    but this error was returned:




    Could not open input file: myfile.php?type=daily




    What can I do?










    share|improve this question


























      59












      59








      59


      18






      I have a PHP file that is needed to be run from command line (via crontab). I need to pass type=daily to the file but I don't know how. I tried:



      php myfile.php?type=daily


      but this error was returned:




      Could not open input file: myfile.php?type=daily




      What can I do?










      share|improve this question
















      I have a PHP file that is needed to be run from command line (via crontab). I need to pass type=daily to the file but I don't know how. I tried:



      php myfile.php?type=daily


      but this error was returned:




      Could not open input file: myfile.php?type=daily




      What can I do?







      php command-line command-line-arguments






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 30 '17 at 2:51









      Super Chafouin

      4,11564163




      4,11564163










      asked Jul 26 '11 at 7:33









      hd.hd.

      6,2843993150




      6,2843993150






















          12 Answers
          12






          active

          oldest

          votes


















          112














          The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.



          You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).



          If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and wget and call that from cron:



          #!/bin/sh
          wget http://location.to/myfile.php?type=daily


          Or check in the php file whether it's called from the commandline or not:



          if (defined('STDIN')) 
          $type = $argv[1];
          else
          $type = $_GET['type'];



          (Note: You'll probably need/want to check if $argv actually contains enough variables and such)






          share|improve this answer

























          • Recommended way is to use getopt()

            – ViliusL
            Dec 20 '18 at 10:32


















          50














          Just pass it as normal parameters and access it in PHP using the $argv array.



          php myfile.php daily


          and in myfile.php



          $type = $argv[1];





          share|improve this answer























          • had an error : "undefined offset"

            – Tarek
            Jul 14 '13 at 11:19












          • thanks everything work now.

            – Tarek
            Jul 14 '13 at 11:32






          • 2





            Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');

            – demenvil
            Jul 21 '16 at 9:59


















          12














          These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:



          if (!empty($argv[1])) 
          parse_str($argv[1], $_GET);



          Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.



          See http://php.net/manual/en/function.parse-str.php for details.






          share|improve this answer























          • Perfect answer! Thanks!

            – Reado
            Jan 12 '17 at 13:31











          • This is fantastic - thank you thank you!!

            – Tim Curtin
            Apr 9 '18 at 23:08


















          6














          parameters send by index like other application



          php myfile.php type=daily


          and then you can gat them like this



          <?php
          if (count($argv) == 0) exit;
          foreach ($argv as $arg)
          echo $arg;
          ?>





          share|improve this answer


















          • 3





            this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"

            – spybart
            Feb 26 '16 at 22:15


















          4














          Save this code in file myfile.php and run as php myfile.php type=daily



          <?php
          $a = $argv;
          $b = array();
          if (count($a) == 1) exit;
          foreach ($a as $key => $arg)
          if ($key > 0)
          list($x,$y) = explode('=', $arg);
          $b["$x"] = $y;


          ?>


          If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.






          share|improve this answer




















          • 1





            While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .

            – J. Chomel
            Jul 15 '16 at 14:44











          • Save this code in file myfile.php

            – easyaspi
            Jul 15 '16 at 15:29











          • Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

            – easyaspi
            Jul 15 '16 at 15:35


















          3














          I strongly recommend the use of getopt.



          Documentation at http://php.net/manual/en/function.getopt.php



          If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface






          share|improve this answer

























          • Not what is asked, but a great tool....

            – Brethlosze
            Jan 18 at 15:42


















          3














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



          if(isset($argv))
          foreach ($argv as $arg)
          $e=explode("=",$arg);
          if(count($e)==2)
          $_GET[$e[0]]=$e[1];
          else
          $_GET[$e[0]]=0;






          share|improve this answer






























            3














            Using getopt() function we can also read parameter from command line just pass value with php running command



            php abc.php --name=xyz



            abc.php



            $val = getopt(null, ["name:"]);

            print_r($val);

            o/p:-
            array (
            'name' => 'xyz',
            )






            share|improve this answer






























              2














              <?php
              if (count($argv) == 0) exit;
              foreach ($argv as $arg)
              echo $arg;
              ?>


              This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script



              array(2) 
              [0]=>
              string(13) "phpscript.php"



              This one will always execute since will have 1 or 2 args passe






              share|improve this answer






























                2














                You could use what sep16 on php.net recommends:



                <?php

                parse_str(implode('&', array_slice($argv, 1)), $_GET);

                ?>


                It behaves exactly like you'd expect with cgi-php.



                $ php -f myfile.php type=daily a=1 b=2 b=3


                will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').






                share|improve this answer






























                  2














                  Just pass it as parameters as follows:



                  php test.php one two three


                  and inside test.php:



                  <?php
                  if(isset($argv))

                  foreach ($argv as $arg)

                  echo $arg;
                  echo "rn";


                  ?>





                  share|improve this answer
































                    0














                    if (isset($argv) && is_array($argv)) 
                    $param = array();
                    for ($x=1; $x<sizeof($argv);$x++)
                    $pattern = '#/(.+)=(.+)#i';
                    if (preg_match($pattern, $argv[$x]))
                    $key = preg_replace($pattern, '$1', $argv[$x]);
                    $val = preg_replace($pattern, '$2', $argv[$x]);
                    $_REQUEST[$key] = $val;
                    $$key = $val;





                    I put parameters in $_REQUEST




                    $_REQUEST[$key] = $val;




                    and also usable directly




                    $$key=$val




                    use this like that:




                    myFile.php /key=val







                    share|improve this answer






















                      Your Answer






                      StackExchange.ifUsing("editor", function ()
                      StackExchange.using("externalEditor", function ()
                      StackExchange.using("snippets", function ()
                      StackExchange.snippets.init();
                      );
                      );
                      , "code-snippets");

                      StackExchange.ready(function()
                      var channelOptions =
                      tags: "".split(" "),
                      id: "1"
                      ;
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function()
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled)
                      StackExchange.using("snippets", function()
                      createEditor();
                      );

                      else
                      createEditor();

                      );

                      function createEditor()
                      StackExchange.prepareEditor(
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader:
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      ,
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      );



                      );













                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6826718%2fpass-variable-to-php-script-running-from-command-line%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown

























                      12 Answers
                      12






                      active

                      oldest

                      votes








                      12 Answers
                      12






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      112














                      The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.



                      You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).



                      If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and wget and call that from cron:



                      #!/bin/sh
                      wget http://location.to/myfile.php?type=daily


                      Or check in the php file whether it's called from the commandline or not:



                      if (defined('STDIN')) 
                      $type = $argv[1];
                      else
                      $type = $_GET['type'];



                      (Note: You'll probably need/want to check if $argv actually contains enough variables and such)






                      share|improve this answer

























                      • Recommended way is to use getopt()

                        – ViliusL
                        Dec 20 '18 at 10:32















                      112














                      The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.



                      You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).



                      If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and wget and call that from cron:



                      #!/bin/sh
                      wget http://location.to/myfile.php?type=daily


                      Or check in the php file whether it's called from the commandline or not:



                      if (defined('STDIN')) 
                      $type = $argv[1];
                      else
                      $type = $_GET['type'];



                      (Note: You'll probably need/want to check if $argv actually contains enough variables and such)






                      share|improve this answer

























                      • Recommended way is to use getopt()

                        – ViliusL
                        Dec 20 '18 at 10:32













                      112












                      112








                      112







                      The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.



                      You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).



                      If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and wget and call that from cron:



                      #!/bin/sh
                      wget http://location.to/myfile.php?type=daily


                      Or check in the php file whether it's called from the commandline or not:



                      if (defined('STDIN')) 
                      $type = $argv[1];
                      else
                      $type = $_GET['type'];



                      (Note: You'll probably need/want to check if $argv actually contains enough variables and such)






                      share|improve this answer















                      The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.



                      You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).



                      If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and wget and call that from cron:



                      #!/bin/sh
                      wget http://location.to/myfile.php?type=daily


                      Or check in the php file whether it's called from the commandline or not:



                      if (defined('STDIN')) 
                      $type = $argv[1];
                      else
                      $type = $_GET['type'];



                      (Note: You'll probably need/want to check if $argv actually contains enough variables and such)







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 29 '16 at 16:49









                      Jared Dunham

                      383818




                      383818










                      answered Jul 26 '11 at 7:41









                      PtPazuzuPtPazuzu

                      2,06111210




                      2,06111210












                      • Recommended way is to use getopt()

                        – ViliusL
                        Dec 20 '18 at 10:32

















                      • Recommended way is to use getopt()

                        – ViliusL
                        Dec 20 '18 at 10:32
















                      Recommended way is to use getopt()

                      – ViliusL
                      Dec 20 '18 at 10:32





                      Recommended way is to use getopt()

                      – ViliusL
                      Dec 20 '18 at 10:32













                      50














                      Just pass it as normal parameters and access it in PHP using the $argv array.



                      php myfile.php daily


                      and in myfile.php



                      $type = $argv[1];





                      share|improve this answer























                      • had an error : "undefined offset"

                        – Tarek
                        Jul 14 '13 at 11:19












                      • thanks everything work now.

                        – Tarek
                        Jul 14 '13 at 11:32






                      • 2





                        Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');

                        – demenvil
                        Jul 21 '16 at 9:59















                      50














                      Just pass it as normal parameters and access it in PHP using the $argv array.



                      php myfile.php daily


                      and in myfile.php



                      $type = $argv[1];





                      share|improve this answer























                      • had an error : "undefined offset"

                        – Tarek
                        Jul 14 '13 at 11:19












                      • thanks everything work now.

                        – Tarek
                        Jul 14 '13 at 11:32






                      • 2





                        Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');

                        – demenvil
                        Jul 21 '16 at 9:59













                      50












                      50








                      50







                      Just pass it as normal parameters and access it in PHP using the $argv array.



                      php myfile.php daily


                      and in myfile.php



                      $type = $argv[1];





                      share|improve this answer













                      Just pass it as normal parameters and access it in PHP using the $argv array.



                      php myfile.php daily


                      and in myfile.php



                      $type = $argv[1];






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 26 '11 at 7:35









                      ChrFinChrFin

                      17.7k75391




                      17.7k75391












                      • had an error : "undefined offset"

                        – Tarek
                        Jul 14 '13 at 11:19












                      • thanks everything work now.

                        – Tarek
                        Jul 14 '13 at 11:32






                      • 2





                        Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');

                        – demenvil
                        Jul 21 '16 at 9:59

















                      • had an error : "undefined offset"

                        – Tarek
                        Jul 14 '13 at 11:19












                      • thanks everything work now.

                        – Tarek
                        Jul 14 '13 at 11:32






                      • 2





                        Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');

                        – demenvil
                        Jul 21 '16 at 9:59
















                      had an error : "undefined offset"

                      – Tarek
                      Jul 14 '13 at 11:19






                      had an error : "undefined offset"

                      – Tarek
                      Jul 14 '13 at 11:19














                      thanks everything work now.

                      – Tarek
                      Jul 14 '13 at 11:32





                      thanks everything work now.

                      – Tarek
                      Jul 14 '13 at 11:32




                      2




                      2





                      Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');

                      – demenvil
                      Jul 21 '16 at 9:59





                      Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');

                      – demenvil
                      Jul 21 '16 at 9:59











                      12














                      These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:



                      if (!empty($argv[1])) 
                      parse_str($argv[1], $_GET);



                      Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.



                      See http://php.net/manual/en/function.parse-str.php for details.






                      share|improve this answer























                      • Perfect answer! Thanks!

                        – Reado
                        Jan 12 '17 at 13:31











                      • This is fantastic - thank you thank you!!

                        – Tim Curtin
                        Apr 9 '18 at 23:08















                      12














                      These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:



                      if (!empty($argv[1])) 
                      parse_str($argv[1], $_GET);



                      Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.



                      See http://php.net/manual/en/function.parse-str.php for details.






                      share|improve this answer























                      • Perfect answer! Thanks!

                        – Reado
                        Jan 12 '17 at 13:31











                      • This is fantastic - thank you thank you!!

                        – Tim Curtin
                        Apr 9 '18 at 23:08













                      12












                      12








                      12







                      These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:



                      if (!empty($argv[1])) 
                      parse_str($argv[1], $_GET);



                      Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.



                      See http://php.net/manual/en/function.parse-str.php for details.






                      share|improve this answer













                      These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:



                      if (!empty($argv[1])) 
                      parse_str($argv[1], $_GET);



                      Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.



                      See http://php.net/manual/en/function.parse-str.php for details.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 30 '16 at 10:57









                      fboesfboes

                      1,5991013




                      1,5991013












                      • Perfect answer! Thanks!

                        – Reado
                        Jan 12 '17 at 13:31











                      • This is fantastic - thank you thank you!!

                        – Tim Curtin
                        Apr 9 '18 at 23:08

















                      • Perfect answer! Thanks!

                        – Reado
                        Jan 12 '17 at 13:31











                      • This is fantastic - thank you thank you!!

                        – Tim Curtin
                        Apr 9 '18 at 23:08
















                      Perfect answer! Thanks!

                      – Reado
                      Jan 12 '17 at 13:31





                      Perfect answer! Thanks!

                      – Reado
                      Jan 12 '17 at 13:31













                      This is fantastic - thank you thank you!!

                      – Tim Curtin
                      Apr 9 '18 at 23:08





                      This is fantastic - thank you thank you!!

                      – Tim Curtin
                      Apr 9 '18 at 23:08











                      6














                      parameters send by index like other application



                      php myfile.php type=daily


                      and then you can gat them like this



                      <?php
                      if (count($argv) == 0) exit;
                      foreach ($argv as $arg)
                      echo $arg;
                      ?>





                      share|improve this answer


















                      • 3





                        this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"

                        – spybart
                        Feb 26 '16 at 22:15















                      6














                      parameters send by index like other application



                      php myfile.php type=daily


                      and then you can gat them like this



                      <?php
                      if (count($argv) == 0) exit;
                      foreach ($argv as $arg)
                      echo $arg;
                      ?>





                      share|improve this answer


















                      • 3





                        this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"

                        – spybart
                        Feb 26 '16 at 22:15













                      6












                      6








                      6







                      parameters send by index like other application



                      php myfile.php type=daily


                      and then you can gat them like this



                      <?php
                      if (count($argv) == 0) exit;
                      foreach ($argv as $arg)
                      echo $arg;
                      ?>





                      share|improve this answer













                      parameters send by index like other application



                      php myfile.php type=daily


                      and then you can gat them like this



                      <?php
                      if (count($argv) == 0) exit;
                      foreach ($argv as $arg)
                      echo $arg;
                      ?>






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 26 '11 at 7:40









                      SubdiggerSubdigger

                      1,61331739




                      1,61331739







                      • 3





                        this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"

                        – spybart
                        Feb 26 '16 at 22:15












                      • 3





                        this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"

                        – spybart
                        Feb 26 '16 at 22:15







                      3




                      3





                      this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"

                      – spybart
                      Feb 26 '16 at 22:15





                      this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"

                      – spybart
                      Feb 26 '16 at 22:15











                      4














                      Save this code in file myfile.php and run as php myfile.php type=daily



                      <?php
                      $a = $argv;
                      $b = array();
                      if (count($a) == 1) exit;
                      foreach ($a as $key => $arg)
                      if ($key > 0)
                      list($x,$y) = explode('=', $arg);
                      $b["$x"] = $y;


                      ?>


                      If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.






                      share|improve this answer




















                      • 1





                        While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .

                        – J. Chomel
                        Jul 15 '16 at 14:44











                      • Save this code in file myfile.php

                        – easyaspi
                        Jul 15 '16 at 15:29











                      • Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

                        – easyaspi
                        Jul 15 '16 at 15:35















                      4














                      Save this code in file myfile.php and run as php myfile.php type=daily



                      <?php
                      $a = $argv;
                      $b = array();
                      if (count($a) == 1) exit;
                      foreach ($a as $key => $arg)
                      if ($key > 0)
                      list($x,$y) = explode('=', $arg);
                      $b["$x"] = $y;


                      ?>


                      If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.






                      share|improve this answer




















                      • 1





                        While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .

                        – J. Chomel
                        Jul 15 '16 at 14:44











                      • Save this code in file myfile.php

                        – easyaspi
                        Jul 15 '16 at 15:29











                      • Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

                        – easyaspi
                        Jul 15 '16 at 15:35













                      4












                      4








                      4







                      Save this code in file myfile.php and run as php myfile.php type=daily



                      <?php
                      $a = $argv;
                      $b = array();
                      if (count($a) == 1) exit;
                      foreach ($a as $key => $arg)
                      if ($key > 0)
                      list($x,$y) = explode('=', $arg);
                      $b["$x"] = $y;


                      ?>


                      If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.






                      share|improve this answer















                      Save this code in file myfile.php and run as php myfile.php type=daily



                      <?php
                      $a = $argv;
                      $b = array();
                      if (count($a) == 1) exit;
                      foreach ($a as $key => $arg)
                      if ($key > 0)
                      list($x,$y) = explode('=', $arg);
                      $b["$x"] = $y;


                      ?>


                      If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 19 '18 at 20:43









                      Santiago

                      1,211718




                      1,211718










                      answered Jul 15 '16 at 14:17









                      easyaspieasyaspi

                      411




                      411







                      • 1





                        While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .

                        – J. Chomel
                        Jul 15 '16 at 14:44











                      • Save this code in file myfile.php

                        – easyaspi
                        Jul 15 '16 at 15:29











                      • Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

                        – easyaspi
                        Jul 15 '16 at 15:35












                      • 1





                        While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .

                        – J. Chomel
                        Jul 15 '16 at 14:44











                      • Save this code in file myfile.php

                        – easyaspi
                        Jul 15 '16 at 15:29











                      • Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

                        – easyaspi
                        Jul 15 '16 at 15:35







                      1




                      1





                      While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .

                      – J. Chomel
                      Jul 15 '16 at 14:44





                      While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .

                      – J. Chomel
                      Jul 15 '16 at 14:44













                      Save this code in file myfile.php

                      – easyaspi
                      Jul 15 '16 at 15:29





                      Save this code in file myfile.php

                      – easyaspi
                      Jul 15 '16 at 15:29













                      Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

                      – easyaspi
                      Jul 15 '16 at 15:35





                      Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.

                      – easyaspi
                      Jul 15 '16 at 15:35











                      3














                      I strongly recommend the use of getopt.



                      Documentation at http://php.net/manual/en/function.getopt.php



                      If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface






                      share|improve this answer

























                      • Not what is asked, but a great tool....

                        – Brethlosze
                        Jan 18 at 15:42















                      3














                      I strongly recommend the use of getopt.



                      Documentation at http://php.net/manual/en/function.getopt.php



                      If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface






                      share|improve this answer

























                      • Not what is asked, but a great tool....

                        – Brethlosze
                        Jan 18 at 15:42













                      3












                      3








                      3







                      I strongly recommend the use of getopt.



                      Documentation at http://php.net/manual/en/function.getopt.php



                      If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface






                      share|improve this answer















                      I strongly recommend the use of getopt.



                      Documentation at http://php.net/manual/en/function.getopt.php



                      If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 22 '16 at 18:43

























                      answered Sep 20 '16 at 14:52









                      Francisco LuzFrancisco Luz

                      1,23811425




                      1,23811425












                      • Not what is asked, but a great tool....

                        – Brethlosze
                        Jan 18 at 15:42

















                      • Not what is asked, but a great tool....

                        – Brethlosze
                        Jan 18 at 15:42
















                      Not what is asked, but a great tool....

                      – Brethlosze
                      Jan 18 at 15:42





                      Not what is asked, but a great tool....

                      – Brethlosze
                      Jan 18 at 15:42











                      3














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



                      if(isset($argv))
                      foreach ($argv as $arg)
                      $e=explode("=",$arg);
                      if(count($e)==2)
                      $_GET[$e[0]]=$e[1];
                      else
                      $_GET[$e[0]]=0;






                      share|improve this answer



























                        3














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



                        if(isset($argv))
                        foreach ($argv as $arg)
                        $e=explode("=",$arg);
                        if(count($e)==2)
                        $_GET[$e[0]]=$e[1];
                        else
                        $_GET[$e[0]]=0;






                        share|improve this answer

























                          3












                          3








                          3







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



                          if(isset($argv))
                          foreach ($argv as $arg)
                          $e=explode("=",$arg);
                          if(count($e)==2)
                          $_GET[$e[0]]=$e[1];
                          else
                          $_GET[$e[0]]=0;






                          share|improve this answer













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



                          if(isset($argv))
                          foreach ($argv as $arg)
                          $e=explode("=",$arg);
                          if(count($e)==2)
                          $_GET[$e[0]]=$e[1];
                          else
                          $_GET[$e[0]]=0;







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 9 '18 at 13:05









                          hamboy75hamboy75

                          346212




                          346212





















                              3














                              Using getopt() function we can also read parameter from command line just pass value with php running command



                              php abc.php --name=xyz



                              abc.php



                              $val = getopt(null, ["name:"]);

                              print_r($val);

                              o/p:-
                              array (
                              'name' => 'xyz',
                              )






                              share|improve this answer



























                                3














                                Using getopt() function we can also read parameter from command line just pass value with php running command



                                php abc.php --name=xyz



                                abc.php



                                $val = getopt(null, ["name:"]);

                                print_r($val);

                                o/p:-
                                array (
                                'name' => 'xyz',
                                )






                                share|improve this answer

























                                  3












                                  3








                                  3







                                  Using getopt() function we can also read parameter from command line just pass value with php running command



                                  php abc.php --name=xyz



                                  abc.php



                                  $val = getopt(null, ["name:"]);

                                  print_r($val);

                                  o/p:-
                                  array (
                                  'name' => 'xyz',
                                  )






                                  share|improve this answer













                                  Using getopt() function we can also read parameter from command line just pass value with php running command



                                  php abc.php --name=xyz



                                  abc.php



                                  $val = getopt(null, ["name:"]);

                                  print_r($val);

                                  o/p:-
                                  array (
                                  'name' => 'xyz',
                                  )







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jul 13 '18 at 6:57









                                  RSWRSW

                                  23638




                                  23638





















                                      2














                                      <?php
                                      if (count($argv) == 0) exit;
                                      foreach ($argv as $arg)
                                      echo $arg;
                                      ?>


                                      This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script



                                      array(2) 
                                      [0]=>
                                      string(13) "phpscript.php"



                                      This one will always execute since will have 1 or 2 args passe






                                      share|improve this answer



























                                        2














                                        <?php
                                        if (count($argv) == 0) exit;
                                        foreach ($argv as $arg)
                                        echo $arg;
                                        ?>


                                        This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script



                                        array(2) 
                                        [0]=>
                                        string(13) "phpscript.php"



                                        This one will always execute since will have 1 or 2 args passe






                                        share|improve this answer

























                                          2












                                          2








                                          2







                                          <?php
                                          if (count($argv) == 0) exit;
                                          foreach ($argv as $arg)
                                          echo $arg;
                                          ?>


                                          This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script



                                          array(2) 
                                          [0]=>
                                          string(13) "phpscript.php"



                                          This one will always execute since will have 1 or 2 args passe






                                          share|improve this answer













                                          <?php
                                          if (count($argv) == 0) exit;
                                          foreach ($argv as $arg)
                                          echo $arg;
                                          ?>


                                          This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script



                                          array(2) 
                                          [0]=>
                                          string(13) "phpscript.php"



                                          This one will always execute since will have 1 or 2 args passe







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Mar 10 '13 at 9:55









                                          GrzegorzGrzegorz

                                          1,97711930




                                          1,97711930





















                                              2














                                              You could use what sep16 on php.net recommends:



                                              <?php

                                              parse_str(implode('&', array_slice($argv, 1)), $_GET);

                                              ?>


                                              It behaves exactly like you'd expect with cgi-php.



                                              $ php -f myfile.php type=daily a=1 b=2 b=3


                                              will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').






                                              share|improve this answer



























                                                2














                                                You could use what sep16 on php.net recommends:



                                                <?php

                                                parse_str(implode('&', array_slice($argv, 1)), $_GET);

                                                ?>


                                                It behaves exactly like you'd expect with cgi-php.



                                                $ php -f myfile.php type=daily a=1 b=2 b=3


                                                will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').






                                                share|improve this answer

























                                                  2












                                                  2








                                                  2







                                                  You could use what sep16 on php.net recommends:



                                                  <?php

                                                  parse_str(implode('&', array_slice($argv, 1)), $_GET);

                                                  ?>


                                                  It behaves exactly like you'd expect with cgi-php.



                                                  $ php -f myfile.php type=daily a=1 b=2 b=3


                                                  will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').






                                                  share|improve this answer













                                                  You could use what sep16 on php.net recommends:



                                                  <?php

                                                  parse_str(implode('&', array_slice($argv, 1)), $_GET);

                                                  ?>


                                                  It behaves exactly like you'd expect with cgi-php.



                                                  $ php -f myfile.php type=daily a=1 b=2 b=3


                                                  will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jul 31 '18 at 10:40









                                                  K3---rncK3---rnc

                                                  3,20612134




                                                  3,20612134





















                                                      2














                                                      Just pass it as parameters as follows:



                                                      php test.php one two three


                                                      and inside test.php:



                                                      <?php
                                                      if(isset($argv))

                                                      foreach ($argv as $arg)

                                                      echo $arg;
                                                      echo "rn";


                                                      ?>





                                                      share|improve this answer





























                                                        2














                                                        Just pass it as parameters as follows:



                                                        php test.php one two three


                                                        and inside test.php:



                                                        <?php
                                                        if(isset($argv))

                                                        foreach ($argv as $arg)

                                                        echo $arg;
                                                        echo "rn";


                                                        ?>





                                                        share|improve this answer



























                                                          2












                                                          2








                                                          2







                                                          Just pass it as parameters as follows:



                                                          php test.php one two three


                                                          and inside test.php:



                                                          <?php
                                                          if(isset($argv))

                                                          foreach ($argv as $arg)

                                                          echo $arg;
                                                          echo "rn";


                                                          ?>





                                                          share|improve this answer















                                                          Just pass it as parameters as follows:



                                                          php test.php one two three


                                                          and inside test.php:



                                                          <?php
                                                          if(isset($argv))

                                                          foreach ($argv as $arg)

                                                          echo $arg;
                                                          echo "rn";


                                                          ?>






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Sep 21 '18 at 14:42









                                                          cfnerd

                                                          2,33392332




                                                          2,33392332










                                                          answered Sep 21 '18 at 13:12









                                                          Sam PrasadSam Prasad

                                                          212




                                                          212





















                                                              0














                                                              if (isset($argv) && is_array($argv)) 
                                                              $param = array();
                                                              for ($x=1; $x<sizeof($argv);$x++)
                                                              $pattern = '#/(.+)=(.+)#i';
                                                              if (preg_match($pattern, $argv[$x]))
                                                              $key = preg_replace($pattern, '$1', $argv[$x]);
                                                              $val = preg_replace($pattern, '$2', $argv[$x]);
                                                              $_REQUEST[$key] = $val;
                                                              $$key = $val;





                                                              I put parameters in $_REQUEST




                                                              $_REQUEST[$key] = $val;




                                                              and also usable directly




                                                              $$key=$val




                                                              use this like that:




                                                              myFile.php /key=val







                                                              share|improve this answer



























                                                                0














                                                                if (isset($argv) && is_array($argv)) 
                                                                $param = array();
                                                                for ($x=1; $x<sizeof($argv);$x++)
                                                                $pattern = '#/(.+)=(.+)#i';
                                                                if (preg_match($pattern, $argv[$x]))
                                                                $key = preg_replace($pattern, '$1', $argv[$x]);
                                                                $val = preg_replace($pattern, '$2', $argv[$x]);
                                                                $_REQUEST[$key] = $val;
                                                                $$key = $val;





                                                                I put parameters in $_REQUEST




                                                                $_REQUEST[$key] = $val;




                                                                and also usable directly




                                                                $$key=$val




                                                                use this like that:




                                                                myFile.php /key=val







                                                                share|improve this answer

























                                                                  0












                                                                  0








                                                                  0







                                                                  if (isset($argv) && is_array($argv)) 
                                                                  $param = array();
                                                                  for ($x=1; $x<sizeof($argv);$x++)
                                                                  $pattern = '#/(.+)=(.+)#i';
                                                                  if (preg_match($pattern, $argv[$x]))
                                                                  $key = preg_replace($pattern, '$1', $argv[$x]);
                                                                  $val = preg_replace($pattern, '$2', $argv[$x]);
                                                                  $_REQUEST[$key] = $val;
                                                                  $$key = $val;





                                                                  I put parameters in $_REQUEST




                                                                  $_REQUEST[$key] = $val;




                                                                  and also usable directly




                                                                  $$key=$val




                                                                  use this like that:




                                                                  myFile.php /key=val







                                                                  share|improve this answer













                                                                  if (isset($argv) && is_array($argv)) 
                                                                  $param = array();
                                                                  for ($x=1; $x<sizeof($argv);$x++)
                                                                  $pattern = '#/(.+)=(.+)#i';
                                                                  if (preg_match($pattern, $argv[$x]))
                                                                  $key = preg_replace($pattern, '$1', $argv[$x]);
                                                                  $val = preg_replace($pattern, '$2', $argv[$x]);
                                                                  $_REQUEST[$key] = $val;
                                                                  $$key = $val;





                                                                  I put parameters in $_REQUEST




                                                                  $_REQUEST[$key] = $val;




                                                                  and also usable directly




                                                                  $$key=$val




                                                                  use this like that:




                                                                  myFile.php /key=val








                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Jul 17 '17 at 6:12









                                                                  emmanuelemmanuel

                                                                  191




                                                                  191



























                                                                      draft saved

                                                                      draft discarded
















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid


                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.

                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function ()
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6826718%2fpass-variable-to-php-script-running-from-command-line%23new-answer', 'question_page');

                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      27

                                                                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                                                                      Category:Rhetoric