{"id":508,"date":"2020-04-09T00:39:00","date_gmt":"2020-04-09T04:39:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=508"},"modified":"2021-01-02T00:40:57","modified_gmt":"2021-01-02T05:40:57","slug":"php-how-to-read-command-line-input","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/php-how-to-read-command-line-input\/","title":{"rendered":"PHP: How to read command-line input"},"content":{"rendered":"\n<p>In PHP, all commandline parameter are passed to the $argv array. Following example shows how to use this array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$op1 = $argv&#91;1];\n$op2 = $argv&#91;2];\n$sum = $op1 + $op2;\nprint 'Sum: ' . $sum . \"\\n\";\nprint_r($argv);<\/code><\/pre>\n\n\n\n<p>$argv[1] and $argv[2] contain the passed parameters in the order they were typed. The last line prints a dump of the data where the first parameter is the name of the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ php sum.php 2 3\nSum: 5\nArray\n(\n   &#91;0] =&gt; sum.php\n   &#91;1] =&gt; 2\n   &#91;2] =&gt; 3\n)<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PHP, all commandline parameter are passed to the $argv array. Following example shows how to use this array: $argv[1] and $argv[2] contain the passed parameters in the order they were typed. The last line prints a dump of the data where the first parameter is the name of the file.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[24],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/508\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}