I just started learning php and this is what I have done: I copied the code from the left panel on this page and pasted them into a local html file. But when I open the html in browsers (firefox and chrome), the page doesn't look like what it should be: There is no format and the word 'echo' and some symbols are shown on the page. Why does this happen? How can I fix this issue?
-
Sure you have a web server installed? – u_mulder Aug 12 '16 at 18:22
-
2If you are new in PHP, please read this guide http://www.phptherightway.com/ Dont look at W3School anymore – akDeveloper Aug 12 '16 at 18:24
-
I have XAMPP installed due to some other reasons but obviously it is not running at the moment. Thanks for all the answers and comments! I will go back to look into these resources, and hopefully get the page working soon! – user3768495 Aug 12 '16 at 18:29
-
I wonder when will google strips out w3skull from its searches. – Chay22 Aug 12 '16 at 18:30
-
xampp could be running (or to be clear, apache + mod_php and mysql). See Lal's point about extension. Files have to be .php. You also have to know where your webroot is. Your .php files must be inside (below) the webroot. Last but not least you have to access your page/script via a valid url, which includes, the scheme, host and path. In other words: http://localhost/helloworld.php where helloworld.php is assumed to be the name of your saved script. – gview Aug 12 '16 at 18:34
-
The webroot is a configuration item in apache. It is the directory where scripts and other subdirectories filled with scripts need to be if they are to be directly accessible from your webserver. So you must understand what directory is your webroot, and put all your scripts there (unless they are scripts included in some other script). – gview Aug 12 '16 at 18:36
2 Answers
The extension for a page that has php codes must be .php
and not .html
. Also, for the PHP code to execute you must be running Apache
server on your machine.
This is a good tutorial for beginners, which tells you how to run PHP codes.
The steps are as follows
- Open any Text Editor. Install new if you don't already have any good Text Editor installed. (My Favorite is Sublime Text & Notepad++)
- Write the following PHP Program / CODE in the Text Editor: '; ?> This is test.php File.
- Save the file in XAMPP Installation Directory \ Web Root Directory Note-1: Default XAMPP Installation Directory in Windows is C:\xampp Note-2: Default Web Root Directory in XAMPP is htdocs. All your php files will have to be in this htdocs folder. That means, for a typical installation of XAMPP in Windows, you will have to save the PHP CODE in C:\xampp\htdocs folder.
- When you save the file, name it test.php (just as an example, any valid file name with .php in the end will work). Note: when you save this file, make sure it has no .txt extension at the end. Some text editors place .txt at the end of file name, so it becomes test.php.txt instead of test.php. To avoid this, when you save the file using any text editor, place double quote around the file name: e.g. "test.php"
- Then, go to XAMPP installation folder (typically, C:\xampp) and run xampp-control.exe by double clicking it.
- In the xampp-control window, click the start button beside Apache. Later, if you need other options like MySQL, you'll also have to start MySQL by clicking the start button beside MySQL in the XAMPP control Panel. Note: if your OS hides common file extensions, then you'll see xampp-control, instead of xampp-control.exe
- Now, in your web browser's address bar, type the address: http://localhost/test.php

- 14,726
- 4
- 45
- 70
-
1That was the answer I was about to put, don't know why it was voted down as it can be the only answer given the OP stated they opened the html in the browser. The opening page needs to be .php such as index.php not index.html, then you can include any extension to that page with the code in it. – independent.guru Aug 12 '16 at 18:26
-
-
I don't know why you got a downvote either, although it has disappeared. However, you are also assuming the user is on windows, and that they want to use xamp. – gview Aug 12 '16 at 18:27
-
Since many people don't know why this answer was down-voted i will go ahead explain that. No sorry there is no ***must*** requirement for the page extension to be .php and no you do not necessarily need `Apache`. Think of future readers and you will know why both those statements are misleading. All you need is a web page that any web server is configured to process as a PHP file and that is it. – Hanky Panky Aug 12 '16 at 18:31
-
1@gview the OP has mentioned in his comments that he has already installed XAMPP. – Lal Aug 12 '16 at 18:34
-
@Lal is at least correct about that my file name ends with '.html'. I changed it now but it looks the same in browser (probably because without the server running). – user3768495 Aug 12 '16 at 18:35
-
@Hanky, do you mean I don't need a 'server'? or I need a server but it doesn't have to be Apache? – user3768495 Aug 12 '16 at 18:35
-
You need a web server but it doesnt mean it should always be Apache. Any web server that can process a PHP code file is fine – Hanky Panky Aug 12 '16 at 18:35
-
-
@HankyPanky, it is good to know that there are other types of servers. I think I will go with Apache since it seems to be very popular. Thank you anyway! – user3768495 Aug 12 '16 at 18:38
-
After we discussed, the op did state he had xampp, so that is ok by me now, however it now makes the duplicate demarcation much more relevant. – gview Aug 12 '16 at 18:39
-
As a beginner, the easiest thing would be to use the new built-in server that comes with php, and is covered in the link I provided. Personally speaking, I have a mac now, although I developed with a windows workstation for a long time, but I highly prefer virtualization so as not to litter my system with servers that are unlikely to be your deployment environment for a production site. My last recommendation: look into Vagrant + virtualbox: all free stuff, and will provide you access to prebuilt *nix servers with LAMP installed. You turn it on (with a shared drive) and you are running. – gview Aug 12 '16 at 18:43
PHP is a serverside scripting language. It has to be interpreted by a PHP interpreter that is in some way connected or integrated into an HTTP (web) server. There are a number of different ways to do that, with varying degrees of complexity.
How and where you setup your php + webserver is up to you, and the ways to do that typically start with the operating system you are running your workstation on, assuming you want to do so, locally.
You can go right to here for a jumpstart: http://www.phptherightway.com/#getting_started

- 14,876
- 3
- 46
- 51