Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two php field inside html

Tags:

html

php

I have the following code:

<html>
    <head>
    </head>
    <body>
        <?php
            $year=$_POST["year"];
        ?>
        <div id="wrap">
            <?php
                echo "<h3>".$year."</h3>";
            ?>
        </div>
    </body>
</html>

You can see that I assign the content of $_POST to the variable $year in the first PHP code block. Can I use $year in the second php code block without directly using the $_POST variable again?

like image 461
TrueBlue10 Avatar asked Apr 24 '14 10:04

TrueBlue10


People also ask

Can you embed PHP in HTML?

PHP is the abbreviation of Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page. We can use PHP in HTML code by simply adding a PHP tag without doing any extra work.

Where do I put PHP code in HTML?

You can add PHP tags to your HTML Page. You simply need to enclose the PHP code with the PHP starts tag <?

Why PHP is not working in HTML?

Php files can always read and display HTML code, but HTML does not automatically parse php code. To do so, you will need to make adjustments to your . htaccess file. Once that is done, the php code will display within HTML files without issue.

How does HTML interact with PHP?

PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. Before reading these faqs, it's important you learn how to retrieve variables from external sources. The manual page on this topic includes many examples as well.


2 Answers

Yes. Once you have assigned a variable and it is in scope you can use it again and again, unless you re-assign it, or unassign it.

like image 128
Alex Avatar answered Oct 06 '22 16:10

Alex


Yes.

$year=$_POST["year"];

here your $_POST variable is assigned to another variable $year (it can be given a totally different name, maybe something more readable). It automatically becomes a global variable and you can use it in the page anywhere after assigning.

Here you can learn more about PHP variable scope

like image 37
Tharaka Nuwan Avatar answered Oct 06 '22 15:10

Tharaka Nuwan