settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Characters in Range (ASCII table) - PHP Function Task

+2 votes

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

asked in PHP category by user hues

1 Answer

+1 vote

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
    }
}
answered by user Jolie Ann
...