PHP echo | Print One or More Strings as Output

PHP echo is not exactly a function, but a language construct which prints one or more strings in the output. Since it is a language construct and not a function, it is not mandatory to use parentheses with its argument list. In this article, we will discuss the PHP echo. Also, we will discuss a few examples of using it.

Note: The major difference between print and echo is that print only accepts a single parameter. However, echo accepts multiple parameters.

Syntax

echo ($string);

echo $string1, $string2 … $stringN;

Parameters

The PHP echo accepts multiple parameters in the input. However, on passing multiple parameters, the parentheses should not be added in the function call. Also, you can concatenate multiple strings and variables in the parameters.

Return Value

The PHP echo does not return any value. However, it prints all the parameters as it is without appending newline character.

PHP echo

Examples

Let’s discuss a few examples.

Example 1: Print Simple String

For instance, consider a simple example of printing a string.

<?php
	
	echo ("This is a simple statement");

?>

In the above example, the function will simply print the input string.

OUTPUT:
This is a simple statement

Example 2: Not Using Parentheses

Similarly, you can avoid using the parentheses for the parameter list.

<?php
	
	echo "It works without a parentheses too!";

?>
OUTPUT:
It works without a parentheses too!

Example 3: Printing Variables

Also, you can print the value of variables.

<?php
	
	$stringVariable = "This is a string variable";

	$integerVariable = 10;

	echo $stringVariable;

	echo $integerVariable;
?>

You can print variables by providing the variable as a parameter.

OUTPUT
This is a string variable10

Example 4: Printing Array Elements

Similarly, you can print individual array elements. However, you cannot print the whole array using PHP echo.

<?php
	
	$array = array('a' => 'apple');

	echo $array['a'];
?>
OUTPUT:
apple

Also, you can use the PHP print_r Function to print arrays in PHP.

Example 5: Single Quotes Vs Double Quotes

However, there is a difference between using single and double-quotes.

<?php
	
	$fruit = 'apple';

	echo 'My favorite fruit is $fruit';
	
	echo "My favorite fruit is $fruit"; 
?>

The single quote prints the variable name. However, the double-quote prints the value of the string.

OUTPUT:
My favorite fruit is $fruit 
My favorite fruit is apple

Example 6: Concatenate Multiple Variables/Strings

Similarly, you can concatenate multiple strings and variables while using print. This can be easily done using the dot (.) operator.

<?php
	
	$fruit = 'Apple';

	$anotherFruit = 'Mango';

	echo 'My favourite fruits are '.$fruit.' and '.$anotherFruit;
?>
OUTPUT:
My favourite fruits are Apple and Mango

Example 7: Using the Shortcut Syntax

PHP echo also has a shortcut syntax for printing strings in the output. It is enabled by enabling the short_open_tag configuration. Also, by default, this setting is enabled after PHP 5.4.

<?php
$color = "red";
?>

<p>Roses are <?=$color?></p> 
OUTPUT:
Roses are red

Example 8: Multiple Strings and Variables

Similarly, you can also pass multiple strings and variables to echo. It prints all the parameters as it is without appending them with newline characters.

<?php
	$variable = 10;
	echo "This is ", "a test.", "You have to answer atleast ", $variable." questions.";

?>
OUTPUT:
This is a test.You have to answer atleast 10 questions.

Conclusion

In conclusion, we discussed PHP echo in this article. You can read more about it on the Official PHP Documentation. Additionally, you can learn about more PHP String Functions on Concatly.

Spread the Knowledge
Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *