Bird
0
0

How can you write a PHP program that stores "Hello" and "World" in two variables and then outputs "Hello World!"?

hard📝 Application Q9 of 15
PHP - Basics and Execution Model
How can you write a PHP program that stores "Hello" and "World" in two variables and then outputs "Hello World!"?
A<?php $a = 'Hello'; $b = 'World'; echo $a + " " + $b + "!"; ?>
B<?php $a = Hello; $b = World; echo $a $b !; ?>
C<?php $a = "Hello"; $b = "World"; echo "$a $b!"; ?>
D<?php $a = "Hello"; $b = "World"; echo $a . " " . $b . "!"; ?>
Step-by-Step Solution
Solution:
  1. Step 1: Assign strings to variables correctly

    Variables must have strings in quotes; and C do this correctly.
  2. Step 2: Concatenate variables with space and exclamation

    uses dot operator to join strings with space and exclamation; tries to use double quotes with variables inside, which also works but is not in options.
  3. Step 3: Check syntax errors in other options

    misses quotes and concatenation; uses + which is invalid for strings in PHP.
  4. Final Answer:

    <?php $a = "Hello"; $b = "World"; echo $a . " " . $b . "!"; ?> -> Option D
  5. Quick Check:

    Concatenate strings with dot operator [OK]
Quick Trick: Use dot (.) to join strings and variables [OK]
Common Mistakes:
  • Using + for string join
  • Missing quotes on strings
  • Not adding spaces between words

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes