Running php inside HTML

I´m running a php code inside an HTML code so that depending of the value of a variable a specific CSS file is selected.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name = "viewport" content="width=device-width, initial-scale=1.0">
<title>Soluciones Elisar</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Oswald:wght@300;500;600&display=swap" rel="stylesheet">
<?php
$monitoreo = $_POST["actividad"];
$file='test.html';
$data = $monitoreo;
if ($data == 0) {
echo "<style type=\"text\css\" media=\"screen\">@import \"/css/styles.css\";</style>";
}
if ($data == 1) {
echo "<style type=\"text\css\" media=\"screen\">@import \"/css/styles2.css\";</style>";
}
if ($data == 2) {
echo "<style type=\"text\css\" media=\"screen\">@import \"/css/styles3.css\";</style>";
}
if ($data == 3) {
echo "<style type=\"text\css\" media=\"screen\">@import \"/css/styles4.css\";</style>";
}
if ($data == 4) {
echo "<style type=\"text\css\" media=\"screen\">@import \"/css/styles5.css\";</style>";
}
if ($data == 5) {
echo "<style type=\"text\css\" media=\"screen\">@import \"/css/styles6.css\";</style>";
}
?>
</head>
<body>
However the HTML file on my server ignores the CSS file and just prints
@import \"/css/styles.css\";"; } if ($data == 1) { echo ""; } if ($data == 2) { echo ""; } if ($data == 3) { echo ""; } if ($data == 4) { echo ""; } if ($data == 5) { echo ""; } ?>
I dont know if it´s my code or if I´m misunderstanding how php and HTML work. Thank you
Answer
Solution:
How are you viewing what the server is producing? If you're using the browser's inspector, bear in mind that this does not show the actual source code — it shows the browser's best interpretation of the source code, with invalid HTML tidied up. You can view the actual source code with the "View Source" option in the browser; the usual shortcut in most browsers is Ctrl+U.
If you do that, my guess (I could be wrong) is that you'll see that the PHP isn't running at all, and what's being served to the browser is the actual PHP code you've written. If I do not mistake my guess, your problem is not a coding issue, but a server configuration issue.
The PHP file should be run and parsed on the server, and the output of that PHP should be sent to the browser, to be parsed there. Exactly how you tell the server to run and parse PHP varies from server to server, but usually it starts with naming the file with the.php
extension (if you have full control of the server config, you can tell it to treat.html
or even.asp
or.whatever
files as PHP if you want, but this is unusual and confusing, so you probably shouldn't). You'll also need to ensure that PHP is installed on the server, and that the server's PHP module is enabled for the site. You know your server better than we do, and can search for appropriate tutorials.
Answer
Solution:
Try writing your code with single and double quotes like:
echo '<div class="card">';
It worked for me earlier when I had issues with php.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.