PHP Echo for beginners and professionals with examples, php file, php session, php date, php array, php form, functions, time, xml, ajax, php mysql, regex, string, oop
PHP echo
is a language construct, not a function. This means you donβt need to use parentheses with it, though if you want to use multiple parameters, parentheses are required.
echo
:void echo ( string $arg1 [, string $... ] )
The echo
statement is used to print strings, multi-line strings, escape characters, variables, arrays, etc. Here are some important things you should know about it:
echo
is a statement, used to display output.echo
with or without parentheses: echo()
and echo
.echo
does not return any value.,
).echo
is faster than the print
statement. β‘echo
: Printing a String πFile: echo1.php
<?php
echo "Hello by PHP echo";
?>
Output:
Hello by PHP echo
echo
: Printing a Multi-line String πFile: echo2.php
<?php
echo "Hello by PHP echo
this is multi line
text printed by
PHP echo statement
";
?>
Output:
Hello by PHP echo this is multi line text printed by PHP echo statement
Notice how the newlines in the code are ignored by the echo
statement, and the entire content is printed on a single line. π
echo
: Printing Escape Characters π§³File: echo3.php
<?php
echo "Hello escape \"sequence\" characters";
?>
Output:
Hello escape "sequence" characters
The escape character \"
allows you to print the double quotes "
inside the string. π―
echo
: Printing Variable Values π·οΈFile: echo4.php
<?php
$msg = "Hello JavaTpoint PHP";
echo "Message is: $msg";
?>
Output:
Message is: Hello JavaTpoint PHP
This example demonstrates how echo
prints the value of a variable. In this case, it outputs the content of the $msg
variable. π€