PHP Print

PHP Print

PHP Print

In PHP, print is another language construct used to output one or more strings to the browser or to the command line. The basic syntax of the print statement is:

				
					print "string to be printed";
				
			

Here, “string to be printed” can be a string literal or a variable that contains a string value, similar to the echo statement.

For example:

				
					<?php
$name = "John";
print "Hello, " . $name . "!";
?>

				
			

In this example, the print statement outputs the string “Hello, John!” to the browser.

Like the echo statement, you can also use the print statement to output multiple strings separated by commas:

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

				
			

In this example, the print statement outputs the string “Hello, world!” to the browser.

The main difference between echo and print is that echo has no return value, while print returns a value of 1, which can be useful in certain situations. Additionally, echo is considered to be slightly faster than print, since it does not return a value. However, in most cases, the difference in performance between echo and print is negligible, and you can use either one depending on your personal preference.

Join To Get Our Newsletter
Spread the love