Mjukis Mjukis - 7 months ago 27
PHP Question

Decrement character with php

Why is it that it's ok to increment character but not decrement with PHP?


PHP

<?php
$a = "a";
echo $a. "<br>"; //a
echo ++$a. "<br>"; //b
echo --$a. "<br>"; //b
>


Is there a simple way as
--$a
to decrement a charrater?

Answer

There is no direct way to decrement alphabets. But with a simple function you can achieve it:

function decrementLetter($Alphabet) {
    return chr(ord($Alphabet) - 1);
}

Source, thanks to Ryan O'Hara

Comments