# How to use PHP echo() to output text and variables

PHP echo() outputs text immediately **without generating a return value**. This is more efficient than functions like print(). You can use echo() in various contexts to display strings in web pages, files, or other formats

## What is PHP echo?

PHP echo() is a language construct used to output strings on a web page or in a PHP application. With echo() you can display **content on the screen**, be it text, HTML tags, or the value of [PHP variables](https://www.ionos.com/digitalguide/websites/web-development/php-variable/). One advantage of echo() is that the code is often shorter and more readable than constantly opening and closing PHP tags to switch between PHP and HTML

Tip [Deploy Now](https://www.ionos.com/hosting/deploy-now "Deploy Now from IONOS") by IONOS ensures that your projects are hosted on a reliable and powerful infrastructure. Changes to your GitHub repository are automatically committed after the push command. Using modern servers and scalable resources, you can be confident that your web applications will run smoothly and are stable.

## The syntax of PHP echo()

PHP echo accepts a **list of expressions** as parameter:

```php
echo(strings ...$expressions)
```

Since PHP echo is a language construct and not a real function, the **brackets after the keyword are often omitted**. The strings can be enclosed with either single or double quotes.

```php
echo 'Hello World'
```

The PHP echo variable also displays stored text:

```php
$var = "red"
echo $var // Output "red"
```

There’s also a **shorthand** where the expression follows an equal sign and open PHP tag:

```php
<?=$var?>
```

You can find more information about PHP programming in our [PHP tutorial](https://www.ionos.com/digitalguide/websites/website-creation/learn-php-our-all-encompassing-php-tutorial-for-beginners/). Check out our comparisons of [PHP vs. Python](https://www.ionos.com/digitalguide/websites/web-development/php-vs-python/) and [PHP vs. JavaScript](https://www.ionos.com/digitalguide/websites/web-development/php-vs-javascript/).

## Examples for using the PHP echo() function

The echo() function is highly **resource-efficient** with minimal overhead, offering versatile application possibilities.

### Concatenate strings and variables

[PHP operators](https://www.ionos.com/digitalguide/websites/web-development/php-operators/) like the **concatenation operator** `.` allow for combining text and strings stored in variables.

```php
$str1 = 'nice'
$str2 = 'weather'
echo  'What . ' $str1 . ' ' . $str2 .  'today!'
```

You’ll get the following output:

```php
What nice weather today!
```

### Output the value of arrays

You can use PHP echo to display array values:

```php
$colors=array("color=>"blue");
echo "The sky is " . $colors['color'] ;
```

The output is “The sky is blue”, with “blue” retrieved from the [PHP array](https://www.ionos.com/digitalguide/websites/web-development/php-arrays/) “$colors” using the “color” key.

### Using echo() in a PHP class

Custom [PHP classes](https://www.ionos.com/digitalguide/websites/web-development/php-classes/) support **dynamic output** of strings with echo().

```php
class Person {
    private $name;
    public function __construct($name) {
        $this->name = $name;
    }
    public function sayHello() {
        echo "Hello, I am {$this->name}!";
    }
}
$person = new Person("Alice");
$person->sayHello();
```

In the **sayHello() method of the Person class**, the echo() function is employed to create a greeting using the name value provided in the object.

### Database query with PHP echo()

Furthermore, you can [use PHP to retrieve information from a MySQL database](https://www.ionos.com/digitalguide/websites/web-development/use-php-to-retrieve-information-from-a-mysql-mariadb-database/) and display the **results from the query** on the web page with echo().

```php
$sql = "SELECT name, email FROM user WHERE id = 1";
$result = mysqli_query($conn, $sql);
if ($result) {
    $row = mysqli_fetch_assoc($result);
    echo "Name: " . $row['name'] . "<br>";
    echo "E-Mail: " . $row['email'];
} else {
    echo "Error occurred: " . mysqli_error($conn);
}
mysqli_close($conn);
```

In this example we use PHP echo
to separate the output of names and emails with a new line. In case of an error we can link echo() with [PHP functions](https://www.ionos.com/digitalguide/websites/web-development/php-functions/) like mysqli\_error() and display the error description.


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/php-echo/](https://www.ionos.com/digitalguide/websites/web-development/php-echo/) for AI/LLM consumption.