Increment letters like number by certain value in php

前端 未结 2 1735
时光取名叫无心
时光取名叫无心 2021-01-20 15:31

In php if I writ

$c=\'A\';
$c++;
it increments to \'B\' but if I want to increment it with 2 ,3 or more , eg: $c+2 or $c+3 ,

2条回答
  •  盖世英雄少女心
    2021-01-20 16:03

    If you want to increment the character by 2, adding won't work.

    Try this instead:

    echo chr(ord($c) + 2)
    

    Explanation:

    1. Calculate the ascii value of $c using ord($c)
    2. Add the ascii value by 2.
    3. Convert this achii value to string using chr() function.

    Refer to ord() and chr() functions.

    Note: As Mark Baker specifies, this will only work to Z and not beyond.

提交回复
热议问题