How will this php code rubies?

for i in (1..7).reverse_each do
 puts "Left #{i} seconds#{case i 
 when 2..4 then "s" 
 when 1 then "a" 
 else "" 
 end }..."
sleep(1)
end
 puts "Done!
April 4th 20 at 00:47
4 answers
April 4th 20 at 00:49
Solution
<?php

 for ($i = 7; $i >= 1; $i--) {
 $ending = ";
 if ($i === 1) {
 $ending = 'a';
 } elseif ($i >= 2 && $i <= 4) {
 $ending = 's';
}
 echo "Left {$i} seconds{$ending} ...\n";
sleep(1);
}
 echo "Done!";
April 4th 20 at 00:51
Solution
foreach(range(7, 1) as $i) {
 echo "Left {$i} seconds";
 switch($i) {
 case($i >= 2 && $i <=4):
 echo "s";
break;
 case($i == 1):
 echo "a"; 
}
 echo "...", PHP_EOL;
sleep(1);
}
echo "Done!";
switch(true) - Norberto commented on April 4th 20 at 00:54
@Norberto, but I do not care, then he does not need.
simple wrote close to rubislaw code. - Zita commented on April 4th 20 at 00:57
@Zita, obviously, but for range(7, 0) will not work correctly, unlike rubinova - Norberto commented on April 4th 20 at 01:00
@Norberto, plus. - Zita commented on April 4th 20 at 01:03
April 4th 20 at 00:53
Solution
for ($i=7; $i > 0; $i--) { 
 print "Left $i seconds".(($i>=5)?"":($i==1?"a":"s"))."\n";
 // sleep(1);
}
print "Done!\n";
April 4th 20 at 00:55
Solution
for ($i = 7; $i >= 1; $i--) {
 $end = "";
 switch ($i) {
 case 1:
 $end = "and";
break;
 case 2: case 3: case 4:
 $end = "y";
break;
}
 echo "Left $i seconds" . $end;
 // echo "<br>";

sleep(1);
}
echo "Done!";
// echo "<br>";

Find more questions by tags RubyPHP