php

How to combine HTML and PHP?

PHP can be “embedded” in HTML code. In this way, a page can primarily be written in HTML and at the same time supplemented by dynamic code. This is useful, for example, if individual elements on the page are repeated often (loops) or if many subpages contain the same code (variables, constants, etc…). Templates can also be easily created in this way in order to separate the logic and presentation of a page as much as possible.
 

Requirements:

HTML and PHP code can always be mixed together as long as the file is parsed by the PHP compiler. (If this is not the case, then it is returned as normal text, so the PHP code is not executed.) If the webserver is configured correctly, this should be the case for all “.php” files (except those in upload directories and similar). To enable PHP parsing for “.html” files as well, the following line must be added to the .htaccess or the corresponding vHost file in Apache:

AddType application/x-httpd-php .html .htm

Existing HTML files do not need to be modified, the HTML codes provided in this tutorial, they do not contain any text that would otherwise represent the beginning of PHP code.
 

 

Syntax:

The beginning of PHP code is signaled – as usual in all PHP files – by using “<?php”. The end is marked with “?>”. Start and end markers may be contained in a file as often as desired.

There is also a shortened syntax for marking the beginning of PHP code. If this is activated, “<?” can be written instead of “<?php”. The short form can be activated via the php.ini file. In this file the value “short_open_tag” must be set to “1”. Please note that the short form conflicts with the typical startup of XML/XHTML files, for example:

<?xml version="1.0"?>

 

Example 1 : Copyright

In this example a copyright with the current year is displayed. In pure HTML the year would have to be changed constantly, with a little PHP it changes automatically at the turn of the year.
 
PHP:

<html>
    <body>
        <p>This is a sample content</p>
        © <?php echo date('Y'); ?> www.stackhowto.com
    </body>
</html>

HTML:

<html>
    <body>
        <p>This is a sample content</p>
        © 2021 www.stackhowto.com
    </body>
</html>
PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More
 

Example 2 : HTML Table

In this example a table containing all square numbers from 0 to 10 is generated using a PHP loop.
 
PHP:

<html>
    <body>
        All square numbers from 0 to 10:
        <table>
                <?php for ($x=0; $x<=10; $x++): ?>
                    <tr>
                        <td><?php echo $x; ?></td>
                        <td><?php echo $x * $x; ?></td>
                    </tr>
                <?php endfor; ?>
        </table>
    </body>
</html>

HTML:

<html>
    <body>
        All square numbers from 0 to 10:
        <table>
                    <tr>
                        <td>0</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>1</td>
                        <td>1</td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td>4</td>
                    </tr>
                     <tr>
                        <td>3</td>
                        <td>9</td>
                    </tr>
                    <tr>
                        <td>4</td>
                        <td>16</td>
                    </tr>
                    <tr>
                        <td>5</td>
                        <td>25</td>
                    </tr>
                    <tr>
                        <td>6</td>
                        <td>36</td>
                    </tr>
                    <tr>
                        <td>7</td>
                        <td>49</td>
                    </tr>
                    <tr>
                        <td>8</td>
                        <td>64</td>
                    </tr>
                    <tr>
                        <td>9</td>
                        <td>81</td>
                    </tr>
                    <tr>
                        <td>10</td>
                        <td>100</td>
                    </tr>
        </table>
    </body>
</html>

 

 

Example 3: Constants & include

In this example, the most important URLs of a page are exported to a file and saved in this as constants. This file can be integrated on all sub-pages, which in turn can use the constants. The advantage is that the most important URLs are stored centrally. If one of the URLs changes, only one file needs to be adapted instead of many different ones.

The file with the constants (here referred to as “myconstants.php”):
 
PHP: myconstants.php

<?php
    // myconstants.php
    define('CATEGORY_PHP_URL', 'http://www.example.com/php/');
    define('CATEGORY_JAVA_URL', 'http://www.example.com/java/');
    define('CATEGORY_HTML_URL', 'http://www.example.com/html/');
    define('CATEGORY_CSS_URL', 'http://www.example.com/css/');
?>

A page that includes this file and uses the constants:
 
PHP:

<?php include('myconstants.php'); ?>
<html>
    <body>
        <div>
            <a href="<?php echo CATEGORY_PHP_URL ?>">PHP tutorial</a>
            <a href="<?php echo CATEGORY_JAVA_URL ?>">JAVA tutorial</a>
            <a href="<?php echo CATEGORY_HTML_URL ?>">HTML tutorial</a>
            <a href="<?php echo CATEGORY_CSS_URL ?>">CSS tutorial</a>
        </div>
    </body>
</html>
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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