PHP Print function

The PHP print function is a simple yet essential function used to output text or data to a webpage. It is a built-in function in PHP that allows developers to display information on a webpage or a console.

The syntax of the print function is simple, and it takes one argument, which is the data to be printed. Here is the basic syntax:

print “Hello, World!”;

In the above example, the print function is used to display the text “Hello, World!” on the webpage.

The print function can be used to print a variety of data types, including strings, numbers, and variables. Here are some examples:

print “The number is ” . $number;

In this example, the print function is used to display the value of a variable called $number. The . operator is used to concatenate the text string with the variable.

print 42;

In this example, the print function is used to display the number 42 on the webpage.

print true;

In this example, the print function is used to display the boolean value true on the webpage.

One important thing to note is that the print function does not return a value, and it always returns 1. Therefore, it should not be used to assign a value to a variable. Instead, the echo function can be used for this purpose.

The print function can also be used with HTML code to create dynamic webpages. For example, here is how the print function can be used to display a list of items on a webpage:

$items = array("Apples", "Bananas", "Oranges");

print "<ul>";
foreach($items as $item){
print "<li>" . $item . "</li>";
}
print "</ul>";

In this example, an array of items is created, and the print function is used to display them on a webpage. The foreach loop is used to iterate through each item in the array and display it as a list item.

In conclusion, the PHP print function is a useful tool for outputting data to a webpage or console. It is simple to use and can be used with a variety of data types. The print function is often used in conjunction with HTML code to create dynamic webpages that display information in a structured format.

Example for PHP Print function

<?php
print ("Welcome to w3htmlschool online tutorials");
?>