JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.



Print



In PHP the print command construct is used to output just one expression at a time to the client.

An expression can be any value that can be cast to and displayed as a string, for example an expression could be a literal string value, a PHP variable, a functions return value, the result from a math equation, a date or time value, etc.

It is important to understand that the print command is a language construct and is not a function. The argument passed to the print command construct is just one expression, and is not delimited by parentheses.

The PHP print command construct only accepts a single argument, and always returns a value of 1.

The major differences between print and echo command constructs is that echo accepts multiple arguments and does not have a return value, while print only accepts one argument and does have a return value. The echo command construct executes significantly faster than print command construct as well.

Examples

The following examples uses a PHP variable named $employeeCount which contains some numerical value, for example: <?php $employeeCount = 15; ?>

The print command construct can be used without parentheses:

 
<!DOCTYPE html>
<?php
    $employeeCount = 15; // TODO: lookup this value from some database
?>
<html>
<head>
</head>
<body>
    <p>There are <?php print $employeeCount; ?> active employees present.</p>
</body>
</html>

The print command construct can be used with parentheses:

 
<!DOCTYPE html>
<?php
    $employeeCount = 15; // TODO: lookup this value from some database
?>
<html>
<head>
</head>
<body>
    <p>There are <?php print($employeeCount); ?> active employees present.</p>
</body>
</html>

The following show different ways to use the print command construct:

 
<!DOCTYPE html>
<?php
    $employeeCount = 15; // TODO: lookup this value from some database
    $pageTitle = "Hello World!";
?>
<html>
<head>
</head>
<body>
    <?php
      
      print("<h3>" . $pageTitle . "</h3>");
      
      if ($employeeCount <= 0)
      {
          print("<p>The company currently does not have any active employees. Good luck!</p>");
      }
      else
      {
          print("<p>The company currently has " . $employeeCount . " active employees.</p>");
      }
      
      $tempCount = 3; TODO: lookup number of temporary employees, interns, and contractors
      print "<p>The companys total employee count is " . $employeeCount + $tempCount . ".</p>";
      
      print("<p>Lets count to 7:</p>");
      print("<ul>");
      for($i=0; $i<7; $i++)
      {
          print("<li>" . $i + 1 . "</li>");
      }
      print("</ul>");
      
   ?>
</body>
</html>

Output:

Hello World!

The company currently has 15 active employees.

The companys total employee count is 18.

Lets count to 7:



Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2024 - T&J Divisions, LLC, All Rights Reserved