print

(PHP 4, PHP 5, PHP 7, PHP 8)

printOutput a string

Description

print(string $expression): int

Outputs expression.

print is not a function but a language construct. Its argument is the expression following the print keyword, and is not delimited by parentheses.

The major differences to echo are that print only accepts a single argument and always returns 1.

Parameters

expression

The expression to be output. Non-string values will be coerced to strings, even when the strict_types directive is enabled.

Return Values

Returns 1, always.

Examples

Example #1 print examples

<?php
print "print does not require parentheses.";

// No newline or space is added; the below outputs "helloworld" all on one line
print "hello";
print 
"world";

print 
"This string spans
multiple lines. The newlines will be
output as well"
;

print 
"This string spans\nmultiple lines. The newlines will be\noutput as well.";

// The argument can be any expression which produces a string
$foo "example";
print 
"foo is $foo"// foo is example

$fruits = ["lemon""orange""banana"];
print 
implode(" and "$fruits); // lemon and orange and banana

// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
print 7// 42

// Because print has a return value, it can be used in expressions
// The following outputs "hello world"
if ( print "hello" ) {
    echo 
" world";
}

// The following outputs "true"
=== ) ? print 'true' : print 'false';
?>

Notes

Note: Using with parentheses

Surrounding the argument to print with parentheses will not raise a syntax error, and produces syntax which looks like a normal function call. However, this can be misleading, because the parentheses are actually part of the expression being output, not part of the print syntax itself.

<?php
print "hello";
// outputs "hello"

print("hello");
// also outputs "hello", because ("hello") is a valid expression

print(2) * 3;
// outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3
// the print statement sees the whole expression as one argument

if ( print("hello") && false ) {
    print 
" - inside if";
}
else {
    print 
" - inside else";
}
// outputs " - inside if"
// the expression ("hello") && false is first evaluated, giving false
// this is coerced to the empty string "" and printed
// the print construct then returns 1, so code in the if block is run
?>

When using print in a larger expression, placing both the keyword and its argument in parentheses may be necessary to give the intended result:

<?php
if ( (print "hello") && false ) {
    print 
" - inside if";
}
else {
    print 
" - inside else";
}
// outputs "hello - inside else"
// unlike the previous example, the expression (print "hello") is evaluated first
// after outputting "hello", print returns 1
// since 1 && false is false, code in the else block is run

print "hello " && print "world";
// outputs "world1"; print "world" is evaluated first,
// then the expression "hello " && 1 is passed to the left-hand print

(print "hello ") && (print "world");
// outputs "hello world"; the parentheses force the print expressions
// to be evaluated before the &&
?>

Note: Because this is a language construct and not a function, it cannot be called using variable functions, or named arguments.

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial