Bird
Raised Fist0
PHPprogramming~20 mins

Echo statement in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Echo Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP echo statement?
Consider the following PHP code snippet. What will it print?
PHP
<?php
echo "Hello " . "World!";
?>
AHello World!
BHello.World!
CHelloWorld!
DHello + World!
Attempts:
2 left
💡 Hint
The dot (.) operator in PHP joins strings together.
❓ Predict Output
intermediate
2:00remaining
What does this echo statement output?
Look at this PHP code. What will be printed?
PHP
<?php
echo 5 + 3;
?>
A53
B5 + 3
CError
D8
Attempts:
2 left
💡 Hint
PHP evaluates expressions inside echo before printing.
❓ Predict Output
advanced
2:00remaining
What is the output of this PHP echo with variables?
Given the code below, what will echo print?
PHP
<?php
$name = "Alice";
echo "Hello, $name!";
?>
AHello, Alice!
BError
CHello, !
DHello, $name!
Attempts:
2 left
💡 Hint
Double quotes allow variable interpolation in PHP.
❓ Predict Output
advanced
2:00remaining
What error does this PHP echo statement cause?
What error will this PHP code produce?
PHP
<?php
echo 'Hello, $name!';
?>
ASyntax error
BPrints Hello, Alice!
CPrints Hello, $name!
DUndefined variable error
Attempts:
2 left
💡 Hint
Single quotes do not replace variables in PHP strings.
🧠 Conceptual
expert
2:00remaining
How many items are printed by this PHP echo statement?
What is the number of separate items printed by this echo statement?
PHP
<?php
echo "Hi", " there", "!";
?>
A2
B3
C1
DError
Attempts:
2 left
💡 Hint
Echo can take multiple comma-separated arguments.

Practice

(1/5)
1. What does the echo statement do in PHP?
easy
A. It stores data in a variable.
B. It creates a function.
C. It prints text or variables to the screen.
D. It deletes a file.

Solution

  1. Step 1: Understand the purpose of echo

    The echo statement is used to output text or variables to the screen in PHP.
  2. Step 2: Compare with other options

    Other options describe different PHP actions like storing data, creating functions, or deleting files, which are not done by echo.
  3. Final Answer:

    It prints text or variables to the screen. -> Option C
  4. Quick Check:

    Echo = print output [OK]
Hint: Echo always shows output on the screen [OK]
Common Mistakes:
  • Thinking echo stores data
  • Confusing echo with function creation
  • Assuming echo deletes files
2. Which of the following is the correct syntax to print the text Hello World using echo in PHP?
easy
A. echo "Hello World";
B. echo (Hello World);
C. echo 'Hello World';
D. echo Hello World;

Solution

  1. Step 1: Check the syntax for echo with text

    Text must be inside quotes (single or double) when printed with echo. echo "Hello World"; uses double quotes and ends with a semicolon.
  2. Step 2: Identify syntax errors in other options

    echo Hello World; misses quotes around text. echo 'Hello World' misses semicolon. echo (Hello World); incorrectly uses parentheses without quotes.
  3. Final Answer:

    echo "Hello World"; -> Option A
  4. Quick Check:

    Text in quotes + semicolon = correct echo syntax [OK]
Hint: Always put text inside quotes and end with semicolon [OK]
Common Mistakes:
  • Forgetting quotes around text
  • Missing semicolon at end
  • Using parentheses incorrectly
3. What will be the output of the following PHP code?
<?php
$name = "Alice";
echo "Hello, $name!";
?>
medium
A. Hello, $name!
B. Hello, Alice!
C. Hello, !
D. Syntax Error

Solution

  1. Step 1: Understand variable interpolation in double quotes

    In PHP, variables inside double quotes are replaced by their values. Here, $name is "Alice".
  2. Step 2: Determine the output of echo

    The echo statement prints "Hello, Alice!" because $name is replaced by "Alice".
  3. Final Answer:

    Hello, Alice! -> Option B
  4. Quick Check:

    Variables in double quotes show values [OK]
Hint: Variables inside double quotes print their values [OK]
Common Mistakes:
  • Expecting variable name printed literally
  • Using single quotes which don't interpolate
  • Assuming syntax error due to variable
4. Identify the error in the following PHP code:
<?php
echo 'Welcome to PHP!";
?>
medium
A. Incorrect use of echo keyword
B. Missing semicolon at the end
C. Variable not defined
D. Mismatched quotes in echo statement

Solution

  1. Step 1: Check the quotes used in echo

    The echo statement starts with a single quote but ends with a double quote, causing mismatched quotes.
  2. Step 2: Understand PHP string syntax

    Strings must start and end with the same type of quote (both single or both double). Mismatched quotes cause syntax errors.
  3. Final Answer:

    Mismatched quotes in echo statement -> Option D
  4. Quick Check:

    Quotes must match in strings [OK]
Hint: Start and end quotes must be the same type [OK]
Common Mistakes:
  • Mixing single and double quotes
  • Forgetting semicolon
  • Assuming variable error instead
5. You want to print the following sentence exactly as it is, including the dollar sign:
The price's $price.
Which echo statement will produce this output correctly?
hard
A. echo "The price's \$price.";
B. echo 'The price's $price.';
C. echo "The price's $price.";
D. echo 'The price's \$price.';

Solution

  1. Step 1: Understand variable interpolation and escaping

    In double quotes, $ starts variable interpolation. To print a literal $, it must be escaped with a backslash (\$).
  2. Step 2: Analyze each option

    echo "The price's $price."; prints "The price's " followed by $price (undefined, empty), resulting in "The price's .". echo 'The price's $price.'; syntax error due to unescaped apostrophe in "price's". echo "The price's \$price."; escapes $ correctly in double quotes (apostrophe fine), printing exactly. echo 'The price's \$price.'; syntax error from unescaped apostrophe (backslash literal if fixed).
  3. Final Answer:

    echo "The price's \$price."; -> Option A
  4. Quick Check:

    Escape $ in double quotes with \$ [OK]
Hint: Escape $ with \$ inside double quotes to print it [OK]
Common Mistakes:
  • Not escaping $ in double quotes
  • Using backslash in single quotes incorrectly
  • Syntax error with unescaped apostrophe in single quotes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard