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?
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.
You can add PHP tags to your HTML Page. You simply need to enclose the PHP code with the PHP starts tag <?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With