W3docs

print

The "print" keyword is used in PHP to output a string to the browser or other output stream. In this article, we will explore the syntax and usage of the

The PHP print Construct: Syntax and Usage

The print construct is used in PHP to output a string to the browser or other output stream. In this article, we will explore the syntax and usage of the print construct in depth, and provide plenty of examples to help you master this important PHP feature.

Syntax

The print construct is used to output a string to the browser or other output stream in PHP. Note that it is a language construct that accepts only one argument and always returns 1. Here is the basic syntax for using it:

The PHP syntax of print

print "Hello, world!";

In this example, we use the print construct to output the string "Hello, world!" to the browser or other output stream.

Examples

Let's look at some practical examples of how the print construct can be used:

Examples of PHP print

<?php

// Example 1
$name = "John";
print "Hello, $name!" . PHP_EOL;

// Output: Hello, John!

// Example 2
$result = print "Success";
echo $result; // Outputs: 1

In these examples, we use the print construct to output strings to the browser or other output stream.

Benefits

Using the print construct has several benefits, including:

  • Guaranteed return value: Unlike echo, print always returns 1, allowing it to be used in expressions or assignments.
  • Simplicity: It provides a straightforward way to output a single string or value without complex syntax.

Conclusion

In conclusion, the print construct is a reliable tool for PHP developers who need to output strings or values to the browser or other output stream. It allows you to output data simply and can be used in expressions thanks to its guaranteed return value. We hope this comprehensive guide has been helpful, and we wish you the best of luck as you continue to develop your PHP skills.

Practice

Practice

Which of the following statements are true about the print function in PHP?