<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <?php $number = 5; echo "Глобальное число: $number<br>"; function show_local() { $number = 100; echo "Локальное число: $number<br>"; } show_local(); function recursion() { global $number; static $letter = 'А'; if ($number < 14) { echo "$number:$letter | "; $number++; $letter++; recursion(); } } recursion(); ?> </body> </html> 

The $ letter variable is incremented into the recursive function recursion () function time after time, but does not change its value. Why and how to fix it?

  • one
    correct on english А - teran

1 answer 1

Why is that

Oddly enough, the answer to the question you can find in the documentation for the operator ++

PHP follows Perl conventions (as opposed to C) to perform arithmetic operations on character variables. For example, in PHP and Perl $ a = 'Z'; $ a ++; assigns $ a to 'AA', while in C a = 'Z'; a ++; will assign a to the value '[' (the ASCII value of 'Z' is 90, and the ASCII value of '[' is 91). It should be noted that an increment operation can be applied to character variables, while a decrement operation cannot be used, moreover, only ASCII characters (az and AZ) are supported. An attempt to increment / decrement other character variables will have no effect, the original string will remain unchanged.

-

How to fix it?

As follows from the documentation, only ASCII characters are supported, in your case, the Russian letter А is in the text. Replacing it with English, you can achieve the desired behavior. If you wanted to increment the Russian alphabet, you will have to use other methods (with getting the letter code).

PS: I did not know that they can be incrementally: D

  • Thank you) But I still don’t understand how to increment the Russian alphabet by other methods - JustLearn
  • I tried doing this: $ letter = chr (ord ($ letter) +1); - JustLearn am
  • But instead of Russian characters displays question marks - JustLearn