Bird
0
0

You want to write a PHP program that prints "Hello World!" and then prints "Welcome to PHP." on the next line. Which code is correct?

hard📝 Application Q15 of 15
PHP - Basics and Execution Model
You want to write a PHP program that prints "Hello World!" and then prints "Welcome to PHP." on the next line. Which code is correct?
A<pre><?php echo "Hello World!"; echo "Welcome to PHP."; ?></pre>
B<pre><?php echo "Hello World!\nWelcome to PHP."; ?></pre>
C<pre><?php print "Hello World!" print "Welcome to PHP."; ?></pre>
D<pre><?php echo "Hello World!" echo "Welcome to PHP."; ?></pre>
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax for multiple echo statements

    Each echo must end with a semicolon.
    <?php
    echo "Hello World!";
    echo "Welcome to PHP.";
    ?>
    correctly uses two echo statements with semicolons.
  2. Step 2: Check other options for errors

    <?php
    echo "Hello World!\nWelcome to PHP.";
    ?>
    uses \n inside double quotes but PHP does not automatically print new lines in HTML output.
    <?php
    print "Hello World!" print "Welcome to PHP.";
    ?>
    misses semicolon between print statements.
    <?php
    echo "Hello World!" echo "Welcome to PHP.";
    ?>
    misses semicolon after first echo.
  3. Final Answer:

    Two separate echo statements with semicolons -> Option A
  4. Quick Check:

    Separate echo with semicolons for multiple lines [OK]
Quick Trick: Use separate echo lines with semicolons for multiple outputs [OK]
Common Mistakes:
  • Missing semicolons between statements
  • Using print without semicolons
  • Expecting \n to create new line in browser output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes