PHP and HTML can interact with each other in a number of ways. Here are some of the most common methods:
1 2 3 4 5 6 7 8 9 10 | <!DOCTYPE html> < html > < head > < title >My PHP Page</ title > </ head > < body > < h1 >Hello, <? php echo "world"; ?>!</ h1 > </ body > </ html > In this example, the echo statement inside the PHP tags outputs the string "world", which is then displayed in the HTML page. |
1 2 3 4 5 6 7 8 9 10 11 12 | <?php // PHP code to generate HTML output $name = "world"; $html = "<!DOCTYPE html> <html> <head> <title>My PHP Page</title> </head> <body> <h1>Hello, $name !</h1> </body> </html>"; |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!DOCTYPE html> < html > < head > < title >My PHP Page</ title > </ head > < body > < form action="process.php" method="post"> < label for="name">Enter your name:</ label > < input type="text" name="name" id="name"> < button type="submit">Submit</ button > </ form > </ body > </ html > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php // PHP code to process the form data $name = $_POST ["name"]; $html = "<!DOCTYPE html> <html> <head> <title>My PHP Page</title> </head> <body> <h1>Hello, $name !</h1> </body> </html>"; // Send the HTML code to the browser echo $html ; ?> |