PHP Echo And Print - GeeksforGeeks

PHP echo and print are two most language constructs used for output data on the screen. They are not functions but constructs, meaning they do not require parentheses (though parentheses can be used with print). Both are widely used for displaying strings, variables, and HTML content in PHP scripts.

  • echo: Generally faster and can output multiple strings separated by commas.
  • print: Slightly slower, returns a value (1), and only accepts one argument.

Table of Content

  • PHP echo statement
  • PHP print statement
  • Difference between echo and print statements in PHP
  • Conclusion

PHP echo

It is a language construct and never behaves like a function, hence no parenthesis is required. But the developer can use parenthesis if they need. The end of the echo statement is identified by the semi-colon (‘;’). We can use ‘echo‘ to output one or more strings, numbers, variables, values, and results of expressions.

Basic Syntax of echo

The basic syntax of echo is very basic. It can be used without parentheses:

echo "Hello, World!";

Or with parentheses (though not required):

echo("Hello, World!");

Displaying Variables with echo

Displaying variables with echo statements is also as easy as displaying normal strings. The below example shows different ways to display variables with the help of a PHP echo statement.

PHP <?php // Defining Variables $text = "Hello, World!"; $num1 = 10; $num2 = 20; // Using echo to print // value of variables echo $text . "\n"; echo $num1 . "+" . $num2 . "="; echo $num1 + $num2; ?> OutputHello, World! 10+20=30

The (.) operator in the above code can be used to concatenate two strings in PHP and the “\n” is used for a new line and is also known as line-break. We will learn about these in further articles.

Displaying Strings with echo

We can simply use the keyword echo followed by the string to be displayed within quotes. The below example shows how to display strings with PHP.

PHP <?php echo "Hello,This is a display string example!"; ?> OutputHello,This is a display string example!

Displaying Strings as Multiple Arguments with echo

We can pass multiple string arguments to the echo statement instead of a single string argument, separating them by comma (‘,’) operator. For example, if we have two strings i.e “Hello” and “World” then we can pass them as (“Hello”, “World”).

PHP <?php echo "Multiple ","argument ","string!"; ?> OutputMultiple argument string!

PHP print Statement

The PHP print statement is similar to the echo statement and can be used alternative to echo many times. It is also a language construct, so we may not use parenthesis i.e print or print().

Basic Syntax of print

The basic syntax of print is similar to echo, but it can only output one string or variable at a time.

print "Hello, World!";

Or with parentheses (which are optional):

print("Hello, World!");

Displaying Variables with print Statement

Displaying variables with print statements is also the same as that of echo statements. The example below shows how to display variables with the help of a PHP print statement.

PHP <?php // Defining the variables $text = "Hello, World!"; $num1 = 10; $num2 = 20; // Print the value of variables print $text."\n"; print $num1."+".$num2."="; print $num1 + $num2; ?> OutputHello, World! 10+20=30

Displaying String of Text with print Statement

We can display strings with the print statement in the same way we did with echo statements. The only difference is we cannot display multiple strings separated by comma(,) with a single print statement. The below example shows how to display strings with the help of a PHP print statement.

PHP <?php print "Hello, world!"; ?> OutputHello, world!

Difference between echo and print statements in PHP

The main difference between the print and echo statement is that echo does not behave like a function whereas print behaves like a function. The print statement can have only one argument at a time and thus can print a single string. Also, the print statement always returns a value of 1. Like an echo, the print statement can also be used to print strings and variables. Below are some examples of using print statements in PHP:

Note: Both are language constructs in PHP programs that are more or less the same as both are used to output data on the browser screen. The print statement is an alternative to echo.

S.No.

echo statement

print statement

1.

echo accepts a list of arguments (multiple arguments can be passed), separated by commas.

The print accepts only one argument at a time.

2.

It does not return any value.

It returns the value 1.

3.

It displays the outputs of one or more strings separated by commas.

The print outputs only the strings.

4.

It is comparatively faster than the print statement.

It is slower than an echo statement.

Conclusion

One had to master both print and echo function for a PHP developer looking to build a dynamic and efficient web application. By understanding the basic difference between print and echo, leveraging their features, and more and more practice can take your coding skills to the next level, and you can create robust and user-friendly websites.

Comment More info Next Article PHP Data Types https://media.geeksforgeeks.org/auth/avatar.png GeeksforGeeks Improve Article Tags :
  • PHP
  • Web Technologies
  • PHP-basics

Từ khóa » E Echo Php