+ 1

can i add PHP in HTML

in script can I add PHP code ? if yes for which reason? ()forgivemyenglish

24th Jun 2017, 6:43 PM
dimitriDV
dimitriDV - avatar
6 Answers
+ 4
yes you can and if you mean how its super easy : <?php //your code ? >
24th Jun 2017, 6:54 PM
Hetbo.net
Hetbo.net - avatar
+ 2
yes, connecting to a database par example <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) {     die("Connection failed: " . $conn->connect_error); }  echo "Connected successfully"; ?>
24th Jun 2017, 6:53 PM
Elie Douaihy
Elie Douaihy - avatar
+ 2
Hey @Jakub the second part is true and i know that thanks. But JS is like a walking security risk because of it's way of interacting with DOM that's why it's "Rarely" used for back-end purposes and mostly is used for designing. but you don't have to take my word for it, just Google this: "is Javascript a security risk"
25th Jun 2017, 7:19 AM
Mat
Mat - avatar
+ 1
yes you can. Let's say for example that you have a page that take a user's name and displays it in another page. you cannot do that with plain HTML and Javascript is not good for this kind of things (because it has ZERO security) you store the user's name inside a PHP variable and show it elsewhere. Like this: <p>Your Name is: <?php $name ?> </p>
24th Jun 2017, 6:54 PM
Mat
Mat - avatar
+ 1
thank u so much
24th Jun 2017, 7:22 PM
dimitriDV
dimitriDV - avatar
+ 1
Mat. You are kinda wrong. I mean, just to show usage of PHP it is ok, but: -js is secure (Lots of websites nowadays use js frameworks for backend) -You should never inject php code into html. You should generate template. (MVT or MVC are used in almost every website you see)
24th Jun 2017, 8:13 PM
Jakub Stasiak
Jakub Stasiak - avatar