Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP字母加减法_Php - Fatal编程技术网

PHP字母加减法

PHP字母加减法,php,Php,我有以下测试代码: <?php $letter = 'A'; $letter++; $letter++; echo $letter.'<br>'; // C $letter++; $letter++; $letter++; echo $letter.'<br>'; // F // how to add plus 3 letters // so that // $letter + 3 => I 试试这个 $letter = ord(

我有以下测试代码:

<?php

$letter = 'A';
$letter++;
$letter++;

echo $letter.'<br>';  // C

$letter++;
$letter++;
$letter++;

echo $letter.'<br>';  // F

// how to add plus 3 letters
// so that 
// $letter + 3 => I
试试这个

      $letter = ord('A')+3;
      echo chr($letter);
也许这会奏效:

$x = 'G';
$y = range('A','Z');
echo $y[array_search($x,$y)+3];

可能有更好的解决方案,但我能想到的最快方法是:

  // get ASCII code of first letter
$ascii = ord('A');

  // echo letter for +3
echo chr($ascii + 3);

请记住,在Z

旧线程之后,您将获得其他符号,但以防有人搜索。创建字母数组,如电子表格中所需的字母

$alphabet=数组('A'、'B'、'C'、'D'、'E'、'F'、'G'、'H'、'I'、'J'、'K'、'L'、'M'、'AY'、'AZ');
//在字母A后面加上36
$val=(数组搜索('A',$alphabet))+36;
回声$alphabet[$val]。“
”;
我不太喜欢这些答案,因为它们没有复制PHP的功能。下面可能是复制它的最简单方法

function addLetters($letter,$lettersToAdd){
   for ($i=0;$i<$lettersToAdd;$i++){
      $letter++;
   }
   return $letter;
}

echo addLetters('G',4);
echo "\n";
echo addLetters('Z',4);
函数addLetters($letter,$lettersToAdd){

对于($i=0;$i每个人都建议添加“3”,但这不起作用!:)有人读过这个问题吗?有人用谷歌搜索过这个问题吗?有人(建议使用$l+3的人)真的花了五秒钟来测试建议的解决方案吗?两种方式都有效;请测试这么简单的三行程序。但请记住,这个解决方案(正如griessi已经提到的)与$x++方法不同。php的increment_string()函数的行为类似于perl
s字符串增量,即
$letter='Z';echo++$letter;`prints
AA
。这根本不能回答问题。