0
0
PHPprogramming~5 mins

Why output functions matter in PHP

Choose your learning style9 modes available
Introduction

Output functions show information to the user. They help your program talk to people.

When you want to display a message on the screen.
When you need to show the result of a calculation.
When you want to print variables to check their values.
When you want to create a simple user interface in the console.
When you want to debug your program by seeing what it is doing.
Syntax
PHP
echo 'text';
print 'text';
print_r($variable);

echo and print show text or variables.

print_r is useful to display arrays or objects.

Examples
Prints a simple text message.
PHP
echo 'Hello, world!';
Shows how to print a variable inside text using double quotes.
PHP
$name = 'Alice';
echo "Hello, $name!";
Prints the contents of an array in a readable way.
PHP
$arr = [1, 2, 3];
print_r($arr);
Sample Program

This program prints a greeting with a name and then prints an array of colors.

PHP
<?php
// This program shows a greeting message
$name = 'Bob';
echo "Hello, $name!\n";

// Show an array
$colors = ['red', 'green', 'blue'];
print_r($colors);
?>
OutputSuccess
Important Notes

Use echo for simple output; it is faster and common.

print returns 1, so it can be used in expressions but is slower.

print_r is great for debugging arrays and objects but not for user-friendly output.

Summary

Output functions let your program communicate with users.

Use echo or print to show text or variables.

Use print_r to display arrays or objects for debugging.