1. PHP Print Statement or Function

This Ultimate PHP tutorial explores the basics and usage of print or print() statement, occasionally we call them as a function also, and it has almost a similar function to the echo statement. The PHP print statement is used to display the data output of any type, and we can utilize it to get the output of any variable, array, string, or even HTML markup. Further, print is not a function instead it is a PHP language construct. Moreover, this PHP print statement has similar working just like the echo statement.

Tutorial Contents:

  1. Introduction to Print Statement or Function
  2. Syntax Types of Print Statement
  3. Print Function Examples
  4. Get Current Date and Time Examples
  5. Difference Between Echo & Print
  6. Conclusion

2. Syntax of PHP Print Statement

Primarily, there are two types of syntax of the PHP print statement or function, and there is only a slight difference in parenthesis () usage. Both these syntax variants can be used in our programs like the below instances.

2.1. Syntax Type 1

  • The print keyword
  • The opening " or ' quotation mark
  • The Data to display come after the initial quotation mark
  • Closing " or ' quotation mark
  • Termination with a ; as it is a must in PHP statements

Syntax Type 1

<?php print "Some data here"; ?>

2.2. Syntax Type 2

  • The print keyword
  • The starting parenthesis (
  • The opening " or ' quotation mark
  • The Data to display come after the initial quotation mark
  • Closing " or ' quotation mark
  • The ending part of the parenthesis )
  • Termination with a ; as it is a must in PHP statements

Syntax Type 2

<?php print("Some data here"); ?>

3. PHP Print Statement Example

Since we learned in the introduction of this tutorial that the print statement is used for data output while writing PHP script. Let us discern this concept with the help of simple and in-depth examples.

3.1. String Output Example

The following instance displays a string as an output using this print statement.

Print String Example

<?php

/* Syntax Type 1 */
print "Hello World!";
/* Syntax Type 2 */
print("Learn Web-Development with TutsInsider.");

?>
Hello World!
Learn Web-Development with TutsInsider.

3.2. Variable Output Example

The subsequent example outputs the content of PHP variables using both syntax variants of the print statement. Moreover, any HTML markup that is in between the quotation marks is also rendered on the screen.

Print Variable Example

<?php

$book = "The Great Gatsby";
$writer = "F. Scott Fitzgerald";
$pages = 208;

/* Syntax Type 1 */
print "<strong>Book Title : </strong>" . $book;
/* Syntax Type 2 */
print("<strong>Book Writer : </strong>" . $writer);
print("<strong>Total Pages : </strong>" . $pages);

?>
Book Title : The Great Gatsby
Book Writer : F. Scott Fitzgerald
Total Pages : 208

3.3. Object Output Example

In PHP programming, we can also use the print statement to output the object data as depicted in the below example.

Print Object Example

<?php

/* Object Data Output */
class Tutorial {
	public $name;
	public $difficulty;
	public $time;
}
$tutorial = new Tutorial;
/* Assigning Variable Values */
$tutorial->name = "PHP Tutorial";
$tutorial->difficulty = "Medium";
$tutorial->time = "10 Weeks";

/* Syntax Type 1 */
echo "Name : " .$tutorial->name;
/* Syntax Type 2 */
echo("Difficulty : " .$tutorial->difficulty);
echo("Time : " .$tutorial->time);

?>
Name : PHP Tutorial
Author : John Doe
Difficulty : Medium
Time : 10 Weeks

4. Print Statement For Date & Time

In the previous examples, we have deeply covered the concepts about the use of print statement and displayed the output of various data types. Now we will learn to display the current date and time using this statement using both of its syntax variants.

4.1. Date Output Using PHP print

We will utilize the first syntax type to display the current date.

Date Example

<?php

/* Display current date using 1st syntax */
print date("Y-m-d");

?>

4.2. Time Output Using PHP print()

To obtain the current time, we will use the below expression along with the 2nd type of syntax of the print statement.

Time Example

<?php

/* Display current time using 1st syntax */
print(date("h:i:s A"));

?>

5. Difference Between PHP Echo and Print Statements

In general, both echo and print statements work similarly. Both are interchangeably used, and there is only a matter of preference. Some of the differences between these two language constructs are listed below.

  1. The echo statement is relatively faster than the print statement.
  2. echo can accept multiple parameters, whereas, the print statement takes only a single parameter.
  3. Further, the print statement always returns a value of 1, however, the echo statement does not return any value.
  4. Moreover, the echo is widely supported in different versions of PHP than the print statement.
  5. Lastly, the echo is more used by the developers than the print statement.

6. Conclusion

The above discussion of the PHP print statement explained its usage with various data types. We also used this statement to get the current date and time. Further, we also explored both syntax variants and utilized them in the examples above. Moreover, the key differences between the echo and print functions are also listed in detail.

Give Us Your Feedback
OR
If You Need Any Help!
Contact Us