w3resource
w3resource logo

PHP Exercises : Using a character type variable print numbers

(追記) (追記ここまで)
(追記) (追記ここまで)

19. Arithmetic on Character Variables

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

For more Practice: Solve these Related Problems:

  • Write a PHP script to increment an alphanumeric string and handle rollover when maximum numeric value is reached.
  • Write a PHP script to perform addition on a string with embedded numbers while retaining its alphabetic prefix.
  • Write a PHP script to develop a function that increments the numeric part of an alphanumeric string and formats the result.
  • Write a PHP script to simulate arithmetic operations on a string by adding a fixed value and outputting a new formatted string.

Go to:


PREV : Delay Program Execution.
NEXT : Get Last Error.

PHP Code Editor:



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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

(追記) (追記ここまで)


(追記) (追記ここまで)
(追記) (追記ここまで)


AltStyle によって変換されたページ (->オリジナル) /