PHP - Write program to emulate the function call str_pad($myString, $desiredLength)

Requirements

Write program to emulate the function call str_pad($myString, $desiredLength)

Take a string, and add space characters to the right of it until the string reaches the desired length.

Display both the original and padded string in the page.

Hint

Use a while loop to keep adding spaces to the right of the string until the desired length is reached.

Demo

<?php

        $myString = "Hello, world!";
        $desiredLength = 20;//from  w  ww  .  j  a  v a2s.  c  o m

        echo "Original string:'$myString' \n";

        while (strlen($myString) < 20) {
          $myString .= " ";
        }

        echo "Padded string:  '$myString'";
?>

Result

Related Topic