Print In PHP

Learning how to use PHP print effectively can help you write better code and debug your scripts more effectively, regardless of your level of PHP experience.

In our PHP tutorial the following is a complete guide to using PHP print function.

A PHP print function displays information on the web in a simple, yet versatile manner.

PHP print is a built-in function in PHP, a widely used server-side scripting language for web development. It is used to display output or data on a web page. The print statement is followed by the content that needs to be displayed, enclosed in parentheses or double quotes.



PHP print Function

The print function is a language construct in PHP that is used to output a string value to the browser.

Debugging or displaying the results of a calculation or query is frequently used.

PHP print is similar to echo, but it is a language construct rather than a function, so in some cases it is faster than echo.

Print has a very simple syntax:

print "string";

The print can be used with or without parentheses: print or print().


PHP print Examples

Here is a simple example of how you can output text using the print (note that the text can be formatted in HTML markup):

Example: 

<?php print "<h2>PHP Print Statement!</h2>"; print "PHP is scripting language"; print "Learning PHP is great"; ?>

Example: 

<?php echo "<h2>ChatGPT</h2>"; echo "ChatGPT is a chatbot"; echo "It is Pre-trained Transformer"; ?>

With the print statement, you can output string and int through variables, as shown in the below examples:

Example: 

<?php $string1 = "PHP is great"; $string2 = "Learn PHP from mrexamples.com"; $a = 4; $b = 7; print "<h2>" . $string1 . "</h2>"; print "<p>" . $string2 . "</p>"; print $a + $b; ?>

Example Explaination:

In above example, several variables are created and then displayed using the print function.

The first two variables, $string1 and $string2, are string variables that store text.

The text in these variables is then concatenated with HTML tags using the . operator and displayed using the print function.

Displays the contents of $string1 within an HTML <h2> tag then displays the contents of $string2 within an HTML <p> tag.

The last two lines of the PHP code create two variables, $a and $b, and assign them values of 4 and 7, respectively. The print function is then used to display the result of $a + $b, which is 11.

Example: 

<?php $string1 = "ChatGPT"; $string2 = "Chat generative pre-trained Transformar"; $a = 3.1; $b = 7.4; print "<h2>" . $string1 . "</h2>: <p>" . $string2 . "</p>"; print $a + $b; ?>

You can also use print to show the contents of arrays. Examples include:

Example: 

<?php $names = array("Elon Reeve Musk", "Billgates", "Mark Zuckerberg"); $firms = array("Tesla Inc", "Microsoft", "Facebook"); print $names[0]; print $firms[0]; ?>

Print function in PHP is commonly used for debugging purposes, since it displays values and checks a script’s output.

Conclusion

PHP print function is a simple, yet powerful tool for displaying information in a web-based environment. Whether you’re using it for debugging or simply to display the results of a calculation, print is an essential tool for every PHP developer to know. With its simple syntax and versatility, print is a handy tool to add to your PHP toolkit.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *