Write a php program to demonstrate the different use of echo statement.

With PHP, there are two basic ways to get output: echo and print.

In this tutorial we use echo or print in almost every example. So, this chapter contains a little more info about those two output statements.

PHP echo and print Statements

echo and print are more or less the same. They are both used to output data to the screen.

The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters [although such usage is rare] while print can take one argument. echo is marginally faster than print.

The PHP echo Statement

The echo statement can be used with or without parentheses: echo or echo[].

Display Text

The following example shows how to output text with the echo command [notice that the text can contain HTML markup]:

Example

Try it Yourself »

Definition and Usage

The echo[] function outputs one or more strings.

Note: The echo[] function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo[], using parentheses will generate a parse error.

Tip: The echo[] function is slightly faster than print[].

Tip: The echo[] function also has a shortcut syntax. Prior to PHP 5.4.0, this syntax only works with the short_open_tag configuration setting enabled.

Syntax

Parameter Values

ParameterDescription
strings Required. One or more strings to be sent to the output

Technical Details

Return Value:PHP Version:
No value is returned
4+

More Examples

Example

Write the value of the string variable [$str] to the output:

Try it Yourself »

Example

Write the value of the string variable [$str] to the output, including HTML tags:

Try it Yourself »

Example

Join two string variables together:

 

Try it Yourself »

Example

Write the value of an array to the output:

Try it Yourself »

Example

Write some text to the output:

 

Try it Yourself »

Example

How to use multiple parameters:

 

Try it Yourself »

Example

Difference of single and double quotes. Single quotes will print the variable name, not the value:

Try it Yourself »

Example

Shortcut syntax [will only work with the short_open_tag configuration setting enabled]:

Roses are

Try it Yourself »

❮ PHP String Reference


What is the use of echo statement in PHP?

echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters [although such usage is rare] while print can take one argument.

What is difference between echo and echo in PHP?

echo is similar to print where both are used to output data to the screen. Some of main differences are: Echo can be used in expressions as it has no return value while print has a return value of 1. ... Echo vs Print..

Can you put PHP inside PHP with echo?

Simple answer, No!

Chủ Đề