Echo isn't printing and the whole syntax file is printing instead

I had this working fine for a very long time and nothing has changed since I last left it but now it isn’t working.

The issue is that instead of echoing the objects as programmed it now just print the whole page.

For example on the dev tool it now print;

<?php

echo "testing";

?>

instead of;

testing

into the dev tool.

The only thing I could think of that may caused this issue is that I have updated my Mac OS Catalina from the previous OS version. Could this be the problem?

Are you sure PHP is installed? If the PHP code is not being shown on screen, that means the PHP code isn’t being interpreted as PHP but is interpreted as HTML.

I have XAMPP installed and all files are stored in htdocs. I have been trying to install and reinstalled a few XAMPP versions but it still doesn’t work.

In terminal when php -v it said that my PHP 7.3.11 so I was wondering if it needs to be the right XAMPP version for that?

What URL are you using in your browser? It should be something like http://localhost/your_file.php

I just right clicked from VS on the html file and open with live server which created http://127.0.0.1:5500/index.html automatically to my browser, which is the way I’ve always done it since the start.

Seems everything else works but not the PHP code…

By default, only files with a .php extension will invoke php on the web server.

Okay so I’ve managed to cracked something but it’s still not perfect.

I’ve noticed when clicking on Go live on VS it generated Port: 5500 and so I’ve added
localhost:5500 -> 80 (Over SSH) on XAMPP.

If I click on Go live from VS it doesn’t seems to connect automatically and and only if I type in the URL either 127.0.0.1 or localhost:5500/thatProjectFile then it works. I can see that the PHP code is being interpreted as it’s makes me a lot happier which indicated me that I’m now becoming a better programmer day by day :relaxed:

Down side of this is that whenever I hit save on VS it doesn’t refresh the browser automatically and this could really bugs me as I’m so used to having the live update when coding…

any other solution to solve this?

First, make sure your PHP code is in a .php file and not a .html flie. You schould replace the index.html file with an index.php file. As phdr already mentioned, by default the webserver only interprets PHP code when it’s file extention is .php.

Second, you’ve got XAMPP running. When you click ‘Go Live’ you’re using VS’s integrated server to run the file. What you should be doing is open a browser and simply go to http://localhost/thatProjectFile. Right now, you’re forcing XAMPP to listen to port 5500 and forward all connections to port 80. Which mean that when you click Go Live, you’re being redirected from the Go Live server to Xampp. In other words, you’re no longer using the integraded server. Which is why live update isn’t working. And it won’t work with this setup.
So, either use XAMPP (which means losing live update) or use Go Live (which means you don’t need XAMPP for development), not both. Either is fine, depending on your personal preferences.

PHDR already answered this one! You can NOT view PHP code in an HTML or HTM file. Does not work!
Change the index.html to index.php and it will then work!

Sponsor our Newsletter | Privacy Policy | Terms of Service