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.



Echo



In PHP the echo command construct is used to output one or more expressions 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 echo command is a language construct and is not a function. The arguments passed to the echo command construct is a list of one or more expressions, where each expression is separated by commas (and not delimited by parentheses).

The PHP echo command construct does not have any return value, therefore it cannot be used in the context of an expression.

If you do need to output a string expression and you require a return value when doing so, then I would suggest that you use the PHP Print command construct instead of the PHP echo command construct, since the print command does have a return value of 1 upon successfully printing the output.

The major differences between echo and print 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 echo 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 echo $employeeCount; ?> active employees present.</p>
</body>
</html>

The echo 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 echo($employeeCount); ?> active employees present.</p>
</body>
</html>

The echo command construct can be used in shorthand format:

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

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

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

Output:

Hello World!

The company currently has 15 active employees.

The companys total employee count is 18.

Lets count to 5:



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