How to embed PHP script in the Webpage

The PHP script is used to make a website dynamic.

The code must be written within the defined PHP tags otherwise it would not execute.

With tags, you can display the final output after data manipulation e.g. listing fetch records from MySQL database in the <table>, display value in HTML element, upload image, etc.

How to embed PHP script in the Webpage


Contents

  1. PHP Tags
  2. Include statement
  3. Conclusion

1. PHP Tags

All PHP codes are specified within <?php ?> tag.

Syntax –

<?php
   // code
?>

Example

<html>
     <body>
          <?php
             echo "PHP CODE";
          ?>
     </body>
</html>

2. Include and require

With include and require statement, you can include existing PHP files in your file.

Syntax – include

include "file-path";

Syntax – require

require "file-path";

Both statement use for including file but the only difference is –

The require statement trigger FATAL ERROR if a file included more than once but the include statement doesn’t.

In the example, I have included config.php file on the page. config.php file contains PHP code.

After including the PHP file you can use functions, variables of the including file in your page.

Example

<?php include "config.php"; ?>
<html>
     <body>
          <?php
             echo "PHP CODE";
          ?>
     </body>
</html>

3. Conclusion

Specify your PHP code between <?php ?> tags in the webpage. You can use include and require statements to embed existing PHP files.

If you found this tutorial helpful then don't forget to share.

Leave a Comment