PHP Exercises : Using a character type variable print numbers
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:
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics