PHP echo() Function

The PHP echo function is used to output one or more strings. As echo is not a real function, you are not required to use parenthesis with it.

The PHP echo function is used to output one or more strings. As echo is not a real function, you are not required to use parenthesis with it (If you have only one parameter).

If you have more than one parameters, you should not specify them in a parenthesis. This function doesn’t append a newline to the end of a string.

Syntax

echo(data to output);

Simple echo function

<?php
    $data = "This is PHP echo function.";
    echo $data;
    echo "\n Hello";
?>

Output

This is PHP echo function.

Multi-line text using echo

<pre>
    <?php
        $data = "This is PHP
         echo function
         with multiple-lines";
         echo $data;
    ?>
</pre>

Output

This is PHP
echo function with multiple-lines

echo() with multiple parameter

<?php
    echo 'Hello',' world';
?>

Output

Hello world

Difference between using single quotes and double quotes

If you specify a variable inside single quotes, the name of the variable will be displayed. To display the value of a variable, use double quotes.

Example

<pre>
    <?php
        $a="\nhello"; // \n creates a new line
        echo '$a';
        echo "$a";
        echo $a;
    ?>
</pre>

Output

$a hello hello

Learn more about other PHP string handling functions.


Share Tweet Send
0 Comments
Loading...