PHP echo() Function
PHP echo() function is important string function. It is used to display one or more strings. In another words we can say that the echo() function outputs one or more string.
Syntax:
void echo ( string $arg1 [, string $... ] )
Parameter | Description | Required/Optional |
---|---|---|
str | Specify one or more strings | Required |
Example 1
<?php
$str ="Hello Shorat Innovation";
echo "By using 'echo()' function your string is :".$str;
?>
Output
By using 'echo()' function your string is :Hello Shorat Innovation
Example 2
<?php
$str1 ="Hello JAVA";
$str2 ="Hello Shorat Innovation";
echo "By using 'echo()' function your string is :".$str1.<br>".$str2 ;
?>
Output
By using 'echo()' function your string is :Hello JAVA
Hello Shorat Innovation
Example 3
<!DOCTYPE html>
<html>
<head>
<title>PHP 'echo()' function</title>
</head>
<body>
<?php
$str = "Red";
?>
<p>Your Shirt color is : <?=$str?></p>
</body>
</html>
Output
Your Shirt color is : Red
Example 4
<?php
echo "Your number value is: (1 + 2)".<br>";
echo "By using 'echo()' function: Addition value is: ".(1 + 2).<br>";
?>
Output
Your number value is: (1 + 2)
By using 'echo()' function: Addition value is: 3
Example 5
<?php
echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';
?>
Output
Hello John Doe!
Example 6
<?php
echo "Your string is : 'This ','string ','was ','made ','with multiple parameters.'"."<br>";
echo 'This ','string ','was ','made ','with multiple parameters.';
?>
Output
Your string is : 'This ','string ','was ','made ','with multiple parameters.'
This string was made with multiple parameters.
Example 7
<?php
$age=array("John"=>"35");
echo "John is " . $age['John'] . " years old.";
?>
Output
Peter is 35 years old.