PHP - echo() Function
The PHP echo() function is used to display output, including one or more strings, non-string values, or expressions. It can accept various data types, such as strings, numbers, arrays, and more.
Syntax
Following is the syntax of the PHP echo() function −
echo(string ...$exp): void
Parameters
Following is the parameter of this function −
- exp: One or more expressions to output, which can be strings or other data types, separated by commas.
Return Value
This function does not return any value.
Example 1: Printing a statement
The following program demonstrates the usage of the PHP echo() function −
<?php
#echo "Welcome to Tutorialspoint"; or,
echo ("Welcome to Tutorialspoint");
?>
Output
The above program produces the following output −
Welcome to Tutorialspoint
Example 2: Statement Along with Argument
Following is another example of the PHP echo() function. This function is used to display (print) a statement along with an argument −
<?php
$name = "Aman Gupta";
echo ("My name is $name");
?>
Output
Once the above program is executed, it displays the following output −
My name is Aman Gupta
Example 3: Printing Non-String Expressions
This example shows how to display the result of a non-string argument using the PHP echo() function −
<?php #echo 2*5 or echo (2*5); ?>
Output
Following is the output of the above program −
10
Example 4: Value Based on Condition
The example below uses the PHP echo() function to display a value based on the given condition −
<?php $isValid = true; #echo $isValid ? 'Valid' : 'Invalid'; or echo ($isValid ? 'Valid' : 'Invalid'); ?>
Output
After executing the above program, the following output will be displayed −
Valid