It is necessary to output a line containing spaces to the specified position, in which it is necessary to display its sequence number. For example, for position number 3, the line should contain 2 spaces and the number 3. I am writing the following code:

<? $out = ""; $i = 3; $out[$i] = $i; echo "$out"; ?> 

Displays Array . Is the variable enclosed in double quotes php does not regard as a string? How do I get results?

  • The variable is all right, but you need to access a specific character in a string through curly brackets $ out {$ i}. And the correct decision resulted in the answer. - Indifferent
  • In the documentation I read that it is possible both through figured and through square ones. In any case, does not help. And your answer works! Thank! - co11ter 5:38


1 answer 1

  function SpaceNumberWrite($number) { $out = ''; for($i = 1; $i < $number; $i++) { $out .= ' '; } $out .= $number; return $out; } echo SpaceNumberWrite(1).'<br />'; echo SpaceNumberWrite(2).'<br />'; echo SpaceNumberWrite(3); /* Результат 1 2 3 */