In this tutorial, you shall learn how to print a string in PHP using print() function, with syntax and example programs.
PHP print()
In PHP, print() takes an expression of type string as argument, and outputs the string. If expression is not of type string, then the expression is implicitly converted to a string.
Syntax
The syntax of print() is
print($expression)
where
Parameter | Description |
---|---|
$expression | An expression to be output. Typically a string value. If not a string, then it is converted to a string to print to output. |
print() always returns an integer value of 1
.
Examples
1. Print the string “Hello World”
In the following example, we print a string "Hello World"
to output using print()
.
PHP Program
<?php print("Hello World"); ?>
Output
2. Print a given number
In the following example, we print a string "Hello World"
to output using print()
.
PHP Program
<?php print(3.1415926); ?>
Output
Since, the input to print() is not a string, it internally converts the given value/expression to a string, and prints it to output.
Conclusion
In this PHP Tutorial, we learned how to print a string or an expression to output, using print()
, with examples.