Function corrected

Correction made in :
line 11 :
if($code_array[$i] == "Z")
changed to
if($code_array[$i] == end($chars))

line 23 :
if($code_array[$i -1] != 'Z')
changed to
if($code_array[$i -1] != end($chars))

line 32 :
$code_array[$j] = 0;
changed to 
$code_array[$j] = reset($chars);

Because it detect last and first character/value in array
The generation was wrong.
For example when it should generate string BA, the function generated B0 instead.
Anyway the function is now working correctly.