Bird
Raised Fist0
PHPprogramming~5 mins

Echo statement in PHP

Choose your learning style10 modes available
Introduction

The echo statement is used to show text or values on the screen. It helps you communicate with the user by displaying messages or results.

To display a welcome message on a website.
To show the result of a calculation to the user.
To print variables or text for debugging purposes.
To output HTML content dynamically from PHP code.
Syntax
PHP
echo expression;

The expression can be text in quotes, variables, or a mix of both.

You can use single or double quotes for text, but double quotes allow variables inside the text to be parsed.

Examples
Prints the text Hello, world! on the screen.
PHP
echo "Hello, world!";
Also prints Hello, world! but uses single quotes.
PHP
echo 'Hello, world!';
Prints Hello, Alice! by inserting the variable value inside the text.
PHP
$name = "Alice";
echo "Hello, $name!";
Prints Age: 25 by joining text and a variable with a dot (concatenation).
PHP
$age = 25;
echo "Age: " . $age;
Sample Program

This program sets a name and then prints a welcome message including that name.

PHP
<?php
$name = "Bob";
echo "Welcome, $name!";
?>
OutputSuccess
Important Notes

Echo does not add a new line automatically. Use \n inside double quotes or <br> in HTML to create line breaks.

You can echo multiple items separated by commas, like echo "Hello", " world!";.

Summary

Echo prints text or variables to the screen.

Use quotes for text and variables inside double quotes to show their values.

It is a simple way to communicate with users or show results.

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