Bird
Raised Fist0
PHPprogramming~10 mins

Print 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 text "Hello, World!".

PHP
<?php
[1] "Hello, World!";
?>
Drag options to blanks, or click blank then click option'
Awrite
Bprint
Cprintf
Decho
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' which is not a PHP statement.
Forgetting the semicolon at the end.
Using 'printf' without format specifiers.
2fill in blank
medium

Complete the code to print the value of the variable $name.

PHP
<?php
$name = "Alice";
[1] $name;
?>
Drag options to blanks, or click blank then click option'
Awrite
Bprintline
Cecho
Ddisplay
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'write' or 'display' which are not PHP commands.
Putting quotes around the variable name which would print the variable name literally.
3fill in blank
hard

Fix the error in the code to correctly print "Age: 30".

PHP
<?php
$age = 30;
[1] "Age: " . $age;
?>
Drag options to blanks, or click blank then click option'
Aecho
Bprintline
Cwrite
Ddisplay
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'printline' which does not exist in PHP.
Forgetting to concatenate strings properly.
4fill in blank
hard

Fill both blanks to print the sentence "Hello, John!" using variable $name.

PHP
<?php
$name = "John";
[1] "Hello, " . [2] . "!";
?>
Drag options to blanks, or click blank then click option'
Aecho
B$name
Cprint
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the variable name which prints it literally.
Using 'print' instead of 'echo' (both work but only one is correct here).
5fill in blank
hard

Fill both blanks to print the sentence "The total is 100;" using variable $total.

PHP
<?php
$total = 100;
[1] "The total is " . [2] . ;
?>
Drag options to blanks, or click blank then click option'
Aprint
B$total
C;
Decho
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the semicolon at the end.
Using 'print' instead of 'echo' (both work but only one is correct here).
Putting quotes around the variable name.

Practice

(1/5)
1. What does the print statement do in PHP?
easy
A. It shows text or values on the screen.
B. It saves data to a file.
C. It creates a new variable.
D. It deletes a file.

Solution

  1. Step 1: Understand the purpose of print

    The print statement is used to display output on the screen in PHP.
  2. Step 2: Compare options with print's function

    Only It shows text or values on the screen. correctly describes this behavior; others describe unrelated actions.
  3. Final Answer:

    It shows text or values on the screen. -> Option A
  4. Quick Check:

    Print = Show output [OK]
Hint: Print always outputs text or values to the screen [OK]
Common Mistakes:
  • Thinking print saves or deletes files
  • Confusing print with variable creation
  • Assuming print modifies data silently
2. Which of the following is the correct syntax to print the text Hello World in PHP?
easy
A. print(Hello World);
B. print "Hello World";
C. print "Hello World"
D. print 'Hello World'

Solution

  1. Step 1: Check for quotes around text

    Text must be inside quotes to be printed correctly in PHP.
  2. Step 2: Check for semicolon at the end

    PHP statements must end with a semicolon; only print "Hello World"; has both quotes and semicolon.
  3. Final Answer:

    print "Hello World"; -> Option B
  4. Quick Check:

    Quotes + semicolon = correct print syntax [OK]
Hint: Text must be in quotes and end with semicolon [OK]
Common Mistakes:
  • Omitting quotes around text
  • Missing semicolon at the end
  • Using parentheses without quotes
3. What will be the output of the following PHP code?
print 123;
print " apples";
medium
A. 123apples
B. 123" apples"
C. 123 apples
D. Error: missing semicolon

Solution

  1. Step 1: Analyze first print statement

    print 123; prints the number 123 without quotes.
  2. Step 2: Analyze second print statement

    print " apples"; prints the text ' apples' with a space before apples.
  3. Step 3: Combine outputs

    Both prints run one after another, so output is '123 apples'.
  4. Final Answer:

    123 apples -> Option C
  5. Quick Check:

    Number + text prints together = 123 apples [OK]
Hint: Print numbers without quotes, text with quotes, outputs join [OK]
Common Mistakes:
  • Expecting quotes to print literally
  • Missing space between number and text
  • Thinking missing semicolon causes error here
4. Identify the error in this PHP code:
print "Hello World"
medium
A. Missing semicolon at the end
B. Quotes should be single quotes
C. print should be uppercase
D. Missing parentheses around text

Solution

  1. Step 1: Check statement ending

    PHP statements must end with a semicolon; this code misses it.
  2. Step 2: Verify other syntax parts

    Quotes can be single or double, print is case-insensitive, parentheses optional.
  3. Final Answer:

    Missing semicolon at the end -> Option A
  4. Quick Check:

    Semicolon missing = syntax error [OK]
Hint: Always end print statements with a semicolon [OK]
Common Mistakes:
  • Forgetting semicolon at line end
  • Thinking print must be uppercase
  • Adding unnecessary parentheses
5. How can you print the phrase PHP version: 8.1 using a single print statement with a variable $version = 8.1;?
hard
A. print "PHP version: $version";
B. print 'PHP version: $version';
C. print PHP version: $version;
D. print ("PHP version: " . $version);

Solution

  1. Step 1: Understand variable interpolation in quotes

    Double quotes allow variables inside text to be replaced; single quotes do not.
  2. Step 2: Check each option

    print "PHP version: $version"; would work but misses semicolon; print 'PHP version: $version'; prints variable name literally; print PHP version: $version; is syntax error; print ("PHP version: " . $version); concatenates string and variable correctly with semicolon.
  3. Final Answer:

    print ("PHP version: " . $version); -> Option D
  4. Quick Check:

    Concatenate string and variable with dot and semicolon [OK]
Hint: Use dot (.) to join strings and variables in print [OK]
Common Mistakes:
  • Using single quotes with variables expecting interpolation
  • Omitting semicolon
  • Not concatenating strings and variables properly
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