Be careful when using print. Since print is a language construct and not a function, the parentheses around the argument is not required.
In fact, using parentheses can cause confusion with the syntax of a function and SHOULD be omited.
Most would expect the following behavior:
<?php
if (print("foo") && print("bar")) {
// "foo" and "bar" had been printed
}
?>
But since the parenthesis around the argument are not required, they are interpretet as part of the argument.
This means that the argument of the first print is
("foo") && print("bar")
and the argument of the second print is just
("bar")
For the expected behavior of the first example, you need to write:
<?php
if ((print "foo") && (print "bar")) {
// "foo" and "bar" had been printed
}
?>Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Референца за `function.print.php` со подобрена типографија и навигација.
(PHP 4, PHP 5, PHP 7, PHP 8)
print — Излез стринг
= NULL
Излези expression.
print не е функција туку јазична конструкција. Неговиот аргумент е изразот што следи по print клучниот збор, и не е ограничен со загради.
Главните разлики со echo се дека
print прифаќа само еден аргумент и секогаш враќа
1.
Параметри
expression-
Изразот што треба да се излезе. Вредностите што не се стринг ќе бидат претворени во стрингови, дури и кога the
strict_typesdirective е овозможено.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. 1, секогаш.
Примери
Пример #1 print examples
<?php
print "print does not require parentheses.";
print PHP_EOL;
// No newline or space is added; the below outputs "helloworld" all on one line
print "hello";
print "world";
print PHP_EOL;
print "This string spans
multiple lines. The newlines will be
output as well";
print PHP_EOL;
print "This string spans\nmultiple lines. The newlines will be\noutput as well.";
print PHP_EOL;
// The argument can be any expression which produces a string
$foo = "example";
print "foo is $foo"; // foo is example
print PHP_EOL;
$fruits = ["lemon", "orange", "banana"];
print implode(" and ", $fruits); // lemon and orange and banana
print PHP_EOL;
// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
print 6 * 7; // 42
print PHP_EOL;
// Because print has a return value, it can be used in expressions
// The following outputs "hello world"
if ( print "hello" ) {
echo " world";
}
print PHP_EOL;
// The following outputs "true"
( 1 === 1 ) ? print 'true' : print 'false';
print PHP_EOL;
?>Белешки
Забелешка: Користење со загради
Ставањето на аргументот на
<?php
print "hello";
// outputs "hello"
print("hello");
// also outputs "hello", because ("hello") is a valid expression
print(1 + 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
?>Кога користите
<?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 &&
?>
Забелешка: За автоматско вклучување на датотеки во скрипти, видете исто така Бидејќи ова е конструкција на јазикот, а не функција, не може да се повика користејќи, или именувани аргументи.
Види Исто така
- echo - Излез еден или повеќе стрингови
- printf() Излез на стринг
- flush() Излез на форматиран стринг
- Испразни го баферот за системски излез
Белешки од корисници 3 белешки
The other major difference with echo is that print returns a value, even it’s always 1.
That might not look like much, but you can use print in another expression. Here are some examples:
<?php
rand(0,1) ? print 'Hello' : print 'goodbye';
print PHP_EOL;
print 'Hello ' and print 'goodbye';
print PHP_EOL;
rand(0,1) or print 'whatever';
?>
Here’s a more serious example:
<?php
function test() {
return !!rand(0,1);
}
test() or print 'failed';
?>I wrote a println function that determines whether a \n or a <br /> should be appended to the line depending on whether it's being executed in a shell or a browser window. People have probably thought of this before but I thought I'd post it anyway - it may help a couple of people.
<?php
function println ($string_message) {
$_SERVER['SERVER_PROTOCOL'] ? print "$string_message<br />" : print "$string_message\n";
}
?>
Examples:
Running in a browser:
<?php println ("Hello, world!"); ?>
Output: Hello, world!<br />
Running in a shell:
<?php println ("Hello, world!"); ?>
Output: Hello, world!\n