Bird
Raised Fist0
PHPprogramming~10 mins

Echo statement in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the word Hello.

PHP
<?php
[1] "Hello";
?>
Drag options to blanks, or click blank then click option'
Awrite
Becho
Cshow
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' or 'show' which are not PHP commands.
Forgetting to add a semicolon at the end.
2fill in blank
medium

Complete the code to print the number 123.

PHP
<?php
[1] 123;
?>
Drag options to blanks, or click blank then click option'
Aprint
Bwrite
Cecho
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' or 'show' which are not valid PHP commands.
Trying to use parentheses like a function.
3fill in blank
hard

Fix the error in the code to correctly print Hello World.

PHP
<?php
[1] "Hello World";
?>
Drag options to blanks, or click blank then click option'
Aecho
Bprint
Cecho;
Decho()
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after echo which is not a function.
Forgetting the semicolon at the end.
4fill in blank
hard

Fill both blanks to print the phrase Hello PHP.

PHP
<?php
[1] [2];
?>
Drag options to blanks, or click blank then click option'
Aecho
B"Hello PHP"
C'Hello PHP'
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using print with double quotes but forgetting the semicolon.
Using single quotes when double quotes are expected.
5fill in blank
hard

Fill both blanks to print the sentence Hello, PHP learner!

PHP
<?php
[1] [2] ;
?>
Drag options to blanks, or click blank then click option'
Aecho
B"Hello, PHP learner!"
C;
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the semicolon at the end.
Using print without semicolon.

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