The print Function

The print function outputs the passed string or variable's value. Unlike echo, print is a language construct that always returns 1 and can be used in the context of expressions. The function accepts one mandatory parameter - the string to output.

Syntax

phpprint(string);

Example

Outputting a simple string:

php<?php
  print('Hello World');
?>

Code execution result:

phpHello World

Example

Using print in an expression:

php<?php
  $res = print('Hello') * 10;
  echo $res;
?>

Code execution result:

phpHello10

Example

Outputting a variable's value:

php<?php
  $message = 'Welcome to PHP';
  print($message);
?>

Code execution result:

phpWelcome to PHP

See Also

  • the echo function,
    which also outputs strings
  • the printf function,
    which formats and outputs a string