+ 1
can i add PHP in HTML
in script can I add PHP code ? if yes for which reason? ()forgivemyenglish
6 Answers
+ 4
yes you can and if you mean how
its super easy :
<?php
//your code
? >
+ 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";
?>
+ 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"
+ 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>
+ 1
thank u so much
+ 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)