I have the following test code:
<?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
As shown here, using $letter++ or $letter-- , I can move up or down a character. Is there a way to do something like $letter + 3 , so it adds 3 letters.
$letter++
$letter--
$letter + 3
I know that I can create a function with a loop that adds char to char, and in the end I get the result. But is there a better way?
There may be better solutions, but the fastest way I can think of is:
// get ASCII code of first letter $ascii = ord('A'); // echo letter for +3 echo chr($ascii + 3);
remember that after z
Try it...
$letter = ord('A')+3; echo chr($letter);
Maybe this will work:
$x = 'G'; $y = range('A','Z'); echo $y[array_search($x,$y)+3];
Old thread, but in case someone is looking. Create an array of letters needed for writing, as in a spreadsheet.
$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M',..........'AY','AZ'); //add 36 to the letter A $val = (array_search('A',$alphabet)) + 36; echo $alphabet[$val]."<BR>";
I don't like any of these answers as they do not replicate the PHP function. Below is probably the easiest way to replicate it.
function addLetters($letter,$lettersToAdd){ for ($i=0;$i<$lettersToAdd;$i++){ $letter++; } return $letter; } echo addLetters('G',4); echo "\n"; echo addLetters('Z',4);
gives K AD
Source: https://habr.com/ru/post/1380539/More articles:Difference between jsonstring and json objects - jsonAccess the mutex class in the mscorlib.dll file - mutexHow to lock rows in an InnoDB table? - phpHow to get Visual Studio 2010 to use .NET 4.0 instead of .NET 2.0 in a Windows Phone project? - .netWhy am I not constantly receiving com.android.vending.billing.PURCHASE_STATE_CHANGED notifications? - androidHow to upgrade from Delphi 2007 to XE2? - delphiHow can I delete a large number of phrases in one pass from a large text file? - pythonWpf control overlaps? - wpfOPTIONS request for cross domain recovery using CORS - restnode - restart the server after editing certain files - javascriptAll Articles