Skip to content

print()

Our article is about the PHP language construct print, which is used to output a string. It is similar to the echo construct in PHP. Unlike echo, print returns a value (always 1) and can be used in expressions. In this article, we will discuss the syntax and usage of print, as well as provide some examples.

The print construct is used to output a string. The syntax is as follows:

The PHP syntax of print

php
int print ( string $string )

The construct takes one parameter, $string, which is the string to be output. Note that print accepts only a single argument and always returns 1. Parentheses are optional.

Here is an example of how to use print:

Example of PHP print

php
<?php
print("Hello, World!");
?>

In this example, we use print to output the string "Hello, World!".

The output of this code will be:

console
Hello, World!

As you can see, print has output the string "Hello, World!".

The print construct is a useful tool for outputting strings in PHP. It can help you display text and data on a web page, which is useful for various purposes such as user feedback and debugging. By mastering this construct, you can become a more proficient PHP developer.

We hope this article has been helpful in understanding the print construct in PHP.

Practice

What are the ways to print a string in PHP according to the article on w3docs?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.