[Solved] Characters in Range (ASCII table) - PHP Function Task

  

2
Topic starter

Write a function that receives two characters and prints on a single line all the characters in between them according to the ASCII table:

ascii-table

Examples:

characters in range php function task

1 Answer
2

You can use those 2 PHP functions (explained in the comments in the code):

ord() - Click HERE to read about it

and

chr() - Click HERE to read about it

Also - there is swapping between variables between lines #13 and #17;

You can read how to swap 2 variables in PHP HERE

Here is the code:

<?php
 
$first = readline();
$second = readline();
 
charsRange($first, $second);
 
function charsRange($f, $s) {
//ord() converts ascii value to its corresponding ascii number
    $start = ord($f);
    $end = ord($s);
 
    if ($end < $start) {
        $temp = $end;
        $end = $start; //start is bigger
        $start = $temp; //assigning value of the start
    }
 
    for ($i = $start + 1; $i < $end; $i++) {
        echo chr($i) . " ";//chr() converts ascii number to its corresponding ascii value
    }
}
Share: