Function that returns next character in ASCII table in PHP

0

I would like to make a function in PHP that takes a character and returns the next character, according to the ASCII table.

For example: get 'a' and return 'b'. I know how to do this in C, which would be

char funcao(char C) {
  return C++;
}

But this way it does not work in PHP. Does anyone know how to do it?

    
asked by anonymous 13.02.2018 / 00:39

2 answers

2

Simply convert the value to integer with the ord() function, increment the desired value, and convert to string again with the function chr() .

function charOffset(string $char, int $offset = 3): string
{
    return chr(ord($char) + $offset);
}

So, just call the function:

echo charOffset('a');  // 'd'
echo charOffset('f', 10);  // 'p'

See working at Repl.it | Ideone

    
13.02.2018 / 00:49
0

See if that's what you want.

<?php

$a = ord("a"); #pega o dec de "a"

function transform($transform)
{
    $otro = $transform + 3; #adiciona +3 ao inteiro de $transform
    var_dump(chr($otro));   #mostra na tela o dec de 100 "d"
}

transform($a);
    
13.02.2018 / 00:49