print

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

printAffiche une chaîne de caractères

Description

print ( string $arg ) : int

Affiche le paramètre arg.

print n'est pas vraiment une fonction (c'est techniquement une structure de langage). Cela fait que vous n'êtes pas obligé d'utiliser des parenthèses.

La plus grosse différence avec echo est que print n'accepte qu'un seul argument et retourne toujours 1.

Liste de paramètres

arg

Les données d'entrée.

Valeurs de retour

Retourne 1, toujours.

Exemples

Exemple #1 Exemple avec print

<?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

Entourer l'argument de print avec des parenthèses ne lèvera pas une erreur de syntaxe, et produit une syntaxe ressemblant à un appel normal de fonction. Néanmoins, ceci peut être trompeur, car les parenthèses font en réalité partie de l'expression qui est en cours d'affichage, et non partie de la syntaxe de print en lui-même.

<?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
?>

Quand print est utilisé dans une expression plus large, placer tout deux le mot clé et son argument dans les parenthèses peut être nécessaire pour obtenir le résultat attendu :

<?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: Comme ceci est une structure du langage, et non pas une fonction, il n'est pas possible de l'appeler avec les fonctions variables.

Voir aussi