PHP print() Function
In this chapter you will learn:
- Definition for PHP print() Function
- Syntax for PHP print() Function
- Parameter for PHP print() Function
- Return for PHP print() Function
- Example - Write some text to the output:
- Example - Write the value of the string variable ($str) to the output, including HTML tags
- Example - Join two string variables together
- Example - Write the value of an array to the output
Definition
The print() function outputs one or more strings for various data types.
The parentheses are not required for print statement.
Syntax
PHP print() Function has the following syntax.
print(strings)
Parameter
Parameter | Is Required | Description |
---|---|---|
strings | Required. | One or more strings to be sent to the output |
Return
Always returns 1.
Example 1
Write some text to the output:
<?php //from j ava 2 s . c o m
print "Hello world!";
$str = "\nHello world!\n";
print $str;
?>
The code above generates the following result.
Example 2
Write the value of the string variable ($str) to the output, including HTML tags:
<?php/* jav a 2s .c o m*/
$str = "Hello world from java2s.com!";
print $str;
print "<br>What a nice day!";
?>
The code above generates the following result.
Example 3
Join two string variables together:
<?php/*from ja v a 2 s. com*/
$str1="Hello world from java2s.com!";
$str2="What a nice day!";
print $str1 . " " . $str2;
?>
The code above generates the following result.
Example 4
Write the value of an array to the output:
<?php
$age=array("PHP"=>"5");
print "PHP is " . $age['PHP'] . " years old.";
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP print_r() Function
- Syntax for PHP print_r() Function
- Parameter for PHP print_r() Function
- Return for PHP print_r() Function
- Example - print out for variable
Home » PHP Tutorial » PHP String Functions