The print statement outputs text or variables to the screen. It is used to display information to the user.
Click to reveal answer
beginner
How do you print the text 'Hello, World!' in PHP?
Use print 'Hello, World!'; or echo 'Hello, World!'; to display the text on the screen.
Click to reveal answer
intermediate
What is the difference between print and echo in PHP?
print returns 1 and can be used in expressions, while echo does not return a value. Both output text but echo is slightly faster.
Click to reveal answer
beginner
Can print output variables in PHP?
Yes, you can print variables by including them inside the print statement, for example: print $name;.
Click to reveal answer
beginner
Is it necessary to use parentheses with print in PHP?
No, parentheses are optional. Both print 'text'; and print('text'); work the same way.
Click to reveal answer
Which PHP statement outputs text to the screen?
Ascan
Binput
Cread
Dprint
✗ Incorrect
The print statement outputs text or variables to the screen in PHP.
What will this PHP code output? print 'Hi!';
ANothing
BHi!
CError
Dprint 'Hi!';
✗ Incorrect
The print statement outputs the string inside quotes exactly as it is.
Which of these is true about print in PHP?
A<code>print</code> returns a value
B<code>print</code> cannot output variables
C<code>print</code> is a function that must use parentheses
D<code>print</code> is used to read input
✗ Incorrect
print returns 1, so it can be used in expressions.
Is this valid PHP code? print('Hello');
AOnly valid in PHP 8+
BNo, parentheses cause error
CYes, parentheses are optional
DOnly valid if inside a function
✗ Incorrect
Parentheses around the argument in print are optional and valid.
How do you print the value of a variable $name in PHP?
Aprint $name;
Bprint '$name';
Cprint &name;
Dprint(name);
✗ Incorrect
Use print $name; to output the value stored in the variable.
Explain how the print statement works in PHP and how it differs from echo.
Think about output and return values.
You got /5 concepts.
Write a simple PHP code snippet that prints your name using the print statement.
Use quotes around your name.
You got /5 concepts.
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
Step 1: Understand the purpose of print
The print statement is used to display output on the screen in PHP.
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.
Final Answer:
It shows text or values on the screen. -> Option A
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
Step 1: Check for quotes around text
Text must be inside quotes to be printed correctly in PHP.
Step 2: Check for semicolon at the end
PHP statements must end with a semicolon; only print "Hello World"; has both quotes and semicolon.
Final Answer:
print "Hello World"; -> Option B
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
Step 1: Analyze first print statement
print 123; prints the number 123 without quotes.
Step 2: Analyze second print statement
print " apples"; prints the text ' apples' with a space before apples.
Step 3: Combine outputs
Both prints run one after another, so output is '123 apples'.
Final Answer:
123 apples -> Option C
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
Step 1: Check statement ending
PHP statements must end with a semicolon; this code misses it.
Step 2: Verify other syntax parts
Quotes can be single or double, print is case-insensitive, parentheses optional.
Final Answer:
Missing semicolon at the end -> Option A
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
Step 1: Understand variable interpolation in quotes
Double quotes allow variables inside text to be replaced; single quotes do not.
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.
Final Answer:
print ("PHP version: " . $version); -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.