w3resource

PHP Exercises : Using a character type variable print numbers

PHP : Exercise-19 with Solution

Arithmetic operations on character variables : $d = 'A00'. Using this variable print the following numbers.

Sample Solution: -

PHP Code:

<?php
// Initialize variable $d with value 'A00'
$d = 'A00';

// Loop through 5 iterations
for ($n = 0; $n < 5; $n++) {
    // Increment $d and echo the result
    echo ++$d . "\n";
}
?>

Explanation:

  • Initialize Variable:
    • $d is initialized with the string value 'A00'.
  • Loop through 5 Iterations:
    • A for loop iterates 5 times, with $n starting at 0 and incrementing by 1 until it reaches 4.
  • Increment and Display:
    • In each iteration, ++$d increments $d in "alphanumeric" order.
    • The echo statement outputs the incremented value of $d, followed by a newline (\n).
  • Output Explanation:
    • Each increment follows a pattern similar to an alphanumeric sequence. For example, starting with 'A00', it will output values like A01, A02, A03, A04, and A05.

Output:

A01                                                         
A02                                                         
A03                                                         
A04                                                         
A05

Flowchart:

Flowchart: Using a character type variable print numbers

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to delay the program execution for the given number of seconds.
Next: Write a PHP script to get the last occurred error.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-basic-exercise-19.php