📖 Scenario: Imagine you are creating a simple PHP script to show a message to your website visitors. To see the message, you need to use output functions that send text to the browser.
🎯 Goal: You will build a PHP script that stores a message and then uses an output function to display it on the screen.
📋 What You'll Learn
Create a variable to hold a message string
Create a variable to hold a greeting prefix
Combine the greeting and message using concatenation
Use an output function to display the combined message
💡 Why This Matters
🌍 Real World
Websites use PHP output functions to show text, images, and other content to visitors.
💼 Career
Knowing how to display information is a basic skill for web developers working with PHP.
Progress0 / 4 steps
1
Create a message variable
Create a variable called $message and set it to the string 'Welcome to our website!'.
PHP
Hint
Use the = sign to assign the string to the variable.
2
Create a greeting variable
Create a variable called $greeting and set it to the string 'Hello! ' (note the space after the exclamation mark).
PHP
Hint
Remember to include the space after the exclamation mark inside the quotes.
3
Combine greeting and message
Create a variable called $fullMessage and set it to the combination of $greeting and $message using the concatenation operator ..
PHP
Hint
Use the dot . to join two strings in PHP.
4
Display the combined message
Use the echo statement to output the variable $fullMessage so it shows on the screen.
PHP
Hint
The echo statement sends text to the browser so users can see it.
Practice
(1/5)
1. What is the main purpose of output functions like echo and print in PHP?
easy
A. To display information to the user
B. To perform calculations
C. To store data in variables
D. To create new functions
Solution
Step 1: Understand output functions
Output functions like echo and print are used to show text or variables on the screen.
Step 2: Identify their purpose
They help the program communicate results or messages to the user.
Final Answer:
To display information to the user -> Option A
Quick Check:
Output functions = display info [OK]
Hint: Output functions show info to users, not store or calculate [OK]
Common Mistakes:
Thinking output functions store data
Confusing output with calculations
Assuming output functions create functions
2. Which of the following is the correct syntax to output the text "Hello World" in PHP?
easy
A. echo "Hello World";
B. print(Hello World);
C. echo 'Hello World'
D. print "Hello World"
Solution
Step 1: Check syntax for echo
The correct syntax requires quotes around the string and a semicolon at the end: echo "Hello World";.