print

(PHP 4, PHP 5)

printВыводит строку

Описание

int print ( string $arg )

Выводит arg.

print не является "настоящей" функцией (это конструкция языка), поэтому заключать аргументы в скобки необязательно.

Список параметров

arg

Вводимые данные.

Возвращаемые значения

Возвращает 1, всегда.

Примеры

Пример #1 Примеры использования print

<?php
print("Привет мир!");

print 
"print() можно использовать и без скобок.";

print 
"Это займет
несколько строк. Переводы строки тоже
выводятся"
;

print 
"Это займет\nнесколько строк. Переводы строки тоже\nвыводятся";

print 
"Экранирование символов делается \"Так\".";

// с print можно использовать переменные ...
$foo "foobar";
$bar "barbaz";

print 
"foo - это $foo"// foo - это foobar

// ... и массивы
$bar = array("value" => "foo");

print 
"это {$bar['value']} !"// это foo !

// При использовании одиночных кавычек выводится имя переменной, а не значение
print 'foo - это $foo'// foo - это $foo

// Если вы не используете другие символы, можно вывести просто значения переменных
print $foo;          // foobar

print <<<END
Здесь используется синтаксис "here document" для вывода
нескольких строк с подстановкой переменных 
$variable.
Заметьте, что закрывающий идентификатор должен
располагаться в отдельной строке, никаких пробелов!
END;
?>

Примечания

Замечание: Поскольку это языковая конструкция, а не функция, она не может вызываться при помощи переменных функций.

Смотрите также

Коментарии

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
2008-05-16 07:08:09
http://php5.kiev.ua/manual/ru/function.print.html
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
   
}
?>
2008-08-25 12:45:55
http://php5.kiev.ua/manual/ru/function.print.html
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';   
?>
2023-09-05 07:34:34
http://php5.kiev.ua/manual/ru/function.print.html

    Поддержать сайт на родительском проекте КГБ