URGENT! Write a valid HTML + PHP Page that will count numbers from 1 to 1,000,000?

i. Display every 10th number in the series in Bold
ii. Display every 3rd number in the series in Italics
iii. Bonus: Underline every Prime number in this series.

See my code. Please correct me. Urgent

<?php  
     for ($i = 1; $i <= 1000000; ++$i) {

     $mod3 = $i % 3;
     $mod10 = $i % 10;
     $str = '';
     if (!$mod3) {
       $str = '<p style="color: red;"><u>$i</u></p>';
     }
     if (!$mod10) {
       $str .= '<p style="color: blue;"><u>$i</u></p>';
     }
     if ($mod3 && $mod10) {
       $str .= '<p style="color: purple;"><em>$i</em></p>';
     }
     echo $str, "\n"; // gives: the color is red, blue, purple
    }
?>

$i is a variable. How do you concatenate a string with a variable?

I know that $i is a variable. I tried other means but didnt get the good results. Please assist on time.
Correct me and paste your code here.

Regards.

<b> This in bold</b> <u>This is underline</u> <i>This is italic</i>
Put the above tags in your code
Why have you used
$mod5 = $i %5
t should be
$mod10 = $i%10

We are not here to do your homework for you. You first need to research how to concatenate variables with strings in PHP.

Next, you need to tell me why you are trying to underline numbers which are NOT divisible by 3 OR 5 and trying to italicize numbers which are divisible by 3 and 5? I don’t see anything in your 3 instructions which tells you to do this.

1 Like

Thank you. I have adjust the code a little, See Below. Kindly help me check. @RandellDawson, Pls be a good teacher. I know you can assist. I will appreciate.

<b> This in bold</b> 
<u>This is underline</u> 
<i>This is italic</i>
<?php  
     for ($i = 1; $i <= 1000000; ++$i) {

     $mod3 = $i % 3;
     $mod10 = $i % 10;
     $str = '';
     if (!$mod3) {
       $str = '<p style="color: red;"><u>$i</u></p>';
     }
     if (!$mod10) {
       $str .= '<p style="color: blue;"><u>$i</u></p>';
     }
     if ($mod3 && $mod10) {
       $str .= '<p style="color: purple;"><em>$i</em></p>';
     }
     echo $str, "\n"; // gives: the color is red, blue, purple
    }
?>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

You still are not concatenating the $i with the rest of the string to have it display to the browser. Research how to do that.

1 Like

What of this way?

$str = ‘

$i

’;
$str .= “red”;
echo $str; // gives: the color is red

In a previous reply, I explained how to format your code. Please format it, so that it shows properly.

Thank you.

1 Like

What of this way

$str = '

$i

';

$str .= "red">$i

';

echo $str; // gives: the color is red

```

You need to add one more set of 3 backticks on the line before the code you want to display.

1 Like

$str = '<p style="color";

$str .= "red";

echo $str; // gives: the color is red

...

3 backticks not 3 single quotes.

$str = '<p style="the color is"><u>$i</u></p>';
$str .= "red";

echo $str; // gives: the color is red

You can go back to URGENT! Write a valid HTML + PHP Page that will count numbers from 1 to 1,000,000? and click the edit button to see how I formatted the code with 3 backticks on the line before and the line after the code.

2 Likes

Okay, but how do you concatenate a string in PHP?

The latest code you wrote does concatenate two strings.

After the second line you wrote above, $str now looks like following:

<p style="the color is"><u>$i</u></p>red
1 Like

@RandellDawson
@PriyaMe
@Steffan153

Please check if the code is ok.

<b> This in bold</b> 
<u>This is underline</u> 
<i>This is italic</i>
<?php  
     for ($i = 1; $i <= 1000000; ++$i) {

     $mod3 = $i % 3;
     $mod10 = $i % 10;
     $str = '';
     if (!$mod3) {
       $str = '<p style="the color is"><u>$i</u></p>' red;
     }
     if (!$mod10) {
       $str .= '<p style="the color is"><u>$i</u></p>' blue;
     }
     if ($mod3 && $mod10) {
       $str .= '<p style="the color is"><em>$i</em></p>' purple;
     }
     echo $str, "\n"; // gives: the color is red, blue, purple
    }
?>

Have you tested the output of this code? Does it look correct to you?