-7

I have this html code using PHP and when i run it i get both responces. What am i doing wrong

Here is the HTML/PHP

This is the Output I get


<!DOCTYPE html>
<html>
<body>
<h1> Hello User </h1>

<?php 
$a = 4;
$b = 4; 

if ($a==$b): ?>

<h2> True </h2>

<?php else: ?>

<h2> False </h2>

<?php endif ?>

</body>
</html>

~~~~~
  • 5
    Please add code as text, not an image, so it can be easily copied by anyone. – El_Vanja Mar 29 '20 at 12:32
  • 2
    Duplicated question, Check this answer https://stackoverflow.com/a/722395/2123606 – Yehia Elhawary Mar 29 '20 at 12:33
  • @YehiaElhawary I disagree, OP is using that exact syntax. I cannot reproduce this with the code shown, problem must be elsewhere. – El_Vanja Mar 29 '20 at 12:36
  • 3
    pls, show the output result – Banzay Mar 29 '20 at 12:37
  • Thank you very much but why does this workand mine above not? – user11432150 Mar 29 '20 at 12:37
  • We can't know unless you give us more information. Is this code or output that you have attached? What you wrote should work. Please edit the question to add more information. – El_Vanja Mar 29 '20 at 12:39
  • If you get some strange result, show it to us. However, there's nothing wrong with the posted code. – M. Eriksson Mar 29 '20 at 12:40
  • @YehiaElhawary - How does that answer the OP's question when the answer suggest doing what the OP already does? – M. Eriksson Mar 29 '20 at 12:41
  • @Dlk - The only logical diff between your comment and the OP's code is two vs three equal signs, which shouldn't make any difference in this case. – M. Eriksson Mar 29 '20 at 12:43
  • Updated to show my output – user11432150 Mar 29 '20 at 12:44
  • Can you add the source code as text rather than an image. – Nigel Ren Mar 29 '20 at 12:46
  • Check the page source (right click > view source) and see if you can see the PHP code as well. Btw, how are you running this file? Through a web server or are you opening the php-file directly in your browser from the file system? – M. Eriksson Mar 29 '20 at 12:50
  • Eventually I wanted to run this in a Flask extension but at the moment im saving it as a HTML page and running it using chrome – user11432150 Mar 29 '20 at 12:53
  • Browsers can't run PHP code on their own. You can use [PHP''s built in web server](https://www.php.net/manual/en/features.commandline.webserver.php) while you're developing on your local machine. – M. Eriksson Mar 29 '20 at 13:03
  • @MagnusEriksson Nvm as you said posted code is correct, probably OP forgot to start php server or .php extension. –  Mar 29 '20 at 13:05

1 Answers1

2

The code you wrote works fine. The problem is that the PHP code is not getting executed. Make sure that it is running locally and the file extension is .php since your output is similar to the case of .html file with the same code.

Majed Badawi
  • 27,616
  • 4
  • 25
  • 48