Trying to do:
for($i = 0; $i <= $argc; $i++) { $sum += $argc[$i]; } echo $sum; But it's not right. It is necessary that the console executes this file with a parameter (numbers entered by the user), for example php file.php 235
Trying to do:
for($i = 0; $i <= $argc; $i++) { $sum += $argc[$i]; } echo $sum; But it's not right. It is necessary that the console executes this file with a parameter (numbers entered by the user), for example php file.php 235
If the numbers are written in one word (number), then they need to be divided:
if (isset($argv[1])) { $numbers = trim($argv[1]); $numbers_list = str_split($numbers); $sum = 0; foreach ($numbers_list as $num) { $num = intval($num); $sum += $num; } echo $sum; } file.php (the name of the executable file) will be stored in $argv[0] . Just check: var_dump($argv); - doubleuiTry this simple solution.
unset($argv[0]); echo array_sum(array_filter($argv, 'is_numeric')); Source: https://ru.stackoverflow.com/questions/594439/
All Articles
Но это не правильно- what exactly is meant by this? - Alexey Shimanskyfor($i = 0; $i < strlen($argv[1]); $i++) { $sum += (int)$argv[1][$i]; }for($i = 0; $i < strlen($argv[1]); $i++) { $sum += (int)$argv[1][$i]; }? - Alexey Shimanskyy