abazoskib Posted June 30, 2009 Share Posted June 30, 2009 I know that I am capable of running php files on a CLI, however how can I run specific functions within a php file alone? And moreovr, how do I specify variable values when running a command such as 'php myFile.php'? thanks for the insight in advance. Link to comment https://forums.phpfreaks.com/topic/164305-solved-running-php-functions-on-command-line/ Share on other sites More sharing options...
celsoendo Posted July 1, 2009 Share Posted July 1, 2009 Didn't unerstood your first question. Maybe neither the second one... lol... but when you run php from CLI, you can get the count of parameters with the superglobal $_SERVER['argc'] - arguments count - and you can get all the parameters with the superglobal array $_SERVER['argv'] - arguments values. Example: script.php <?php print 'Total parameters = '. $_SERVER['argc'] . '\n'; print 'Script name = '. $_SERVER['argv'][0] . '\n'; for ($i = 1; $i < $_SERVER['argc']; $i++) { print 'Parameter '. $i . ' = '. $_SERVER['argv'][$i] . '\n'; } ?> Then you can run like this: php script.php first second It will print: Total parameters = 3 Script name = script.php Parameter 1 = first Parameter 2 = second Note that the index 0 from $_SERVER['argv'] is ALWAYS the script name (it's also a parameter). Link to comment https://forums.phpfreaks.com/topic/164305-solved-running-php-functions-on-command-line/#findComment-866843 Share on other sites More sharing options...
abazoskib Posted July 1, 2009 Author Share Posted July 1, 2009 thanks Link to comment https://forums.phpfreaks.com/topic/164305-solved-running-php-functions-on-command-line/#findComment-866909 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.