How to write PHP code in HTML?
|For a experience php web developer it will be a very easy thing. But for a new developer , who recently started learning php programming it will be a questionable thing. So today we will embed php code in regular html code.
Embedding PHP code in regular HTML –
Lets create a hello script named hello.php. For php code, please always save file with .php extension.
<html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html>
In above html code, we printed Hello world in php code. To write php code in html we should use <?php ?> tags. Integrating php and html is very easy thing.
We can write more complex php code in html. We need to just start php code with <?php tag and end with ?> tag.
More advanced example –
<html> <head>PHP HTML Integration</head> <body> <ul> <?php for($i=1;$i<=5;$i++){ ?> <li>Item No <?php echo $i; ?></li> <?php } ?> </ul> </body> </html>
This will out put as –
Item No 1
Item No 2
Item No 3
Item No 4
Item No 5
5 Comments
You did not mention that it must be saved as a .php file. This can be confusing for new PHP coders.
You are right file must be saved as .php . I indicates it as -lets create a file named hello.php.
Tech Blogs I just thought it is best to directly say that it MUST BE a .php file.
Dustin Parker I agree with you and i edited. thanks..
Hello Gaurav,
Nice blog! I am editor at Web Code Geeks (www.webcodegeeks.com). We have the WCG program (see http://www.webcodegeeks.com/join-us/wcg/), that I think you’d be perfect for.
If you’re interested, send me an email to eleftheria.drosopoulou@webcodegeeks.com
and we can discuss further.
Best regards,
Drosopoulou Eleftheria