PHP echo: can't reproduce W3school's code [duplicate] - php

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
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?

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

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

Related

php code not executing instead just showing on page screen [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 3 years ago.
i am trying to execute my php code, instead it just blows it back at my screen. page should be black to execute the code at the background.
I've let other poeple use this same code worked for them.
After setting xampp on your local pc, you've created a local server with apache, mysql, php stack. So when you write any php code, you need to execute/interpret it through your local server.
For example you need to use localhost/phpmailer_test/index.php not file:///C:/xampp/htdocs/phpmailer_test/index.php. If it was a .html file then you can use the file path to execute it but for server side language like php, python and so called server side languages you've to execute your code base through a server.
Seems like you are accessing your file through the file system, which means you are using your browser as something like a text editor. In order to have the php file executed on opening there are several things that you must do:
Set up a server on your computer that can execute PHP. (MAMP or XAMPP are easy to install)
Change the permissions of your server so that you can access it through localhost or 127.0.0.1.
Set up the file system of your server so that you can find your PHP file.
In your case you are using XAMPP, so the htdocs folder should be the 'root' of your local server, then you will be able to access any folder within there by entering the path after htdocs. In your case it should be localhost/phpmailer test/index.php.

PHP file not loading in WAMP localhost [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 5 years ago.
I am new to php and utilizing WAMP platform (version 3.0.6). I have two php files, one is a database connection script to the MySQL databse I created, the other is an html web form with embedded php scripts. Both were created in Microsoft Notepad as my editor and saved with the extension (.php) in the Windows(C:) >wamp64 >www >demoproject folder. While browsing in Windows explorer I noticed the file type is saved as "Text Document", even though the extensions were both saved with .php.
When I launch localhost in my browser and select the project folder listed under Your Projects I receive an error "This site can't be reached". Not sure why this is happening. I am able to open the root directory if I navigate to localhost/demoproject , which from there I can see the two php files (saved as Newspaper2.html.txt and connect-mysql.php.txt). When I launch the newspaper file (html form with php) all I receive is the scripting I created, as opposed to opening the web form. I checked the port for the localhost and I am running on port 80, and I've tried playing with the file extensions a little but it hasn't helped. Apologies if this has been asked before, I didn't see anything specific to this issue. I appreciate any assistance! Thanks.
Files that don't end in .php will not be interpreted unless you have configured their extension to be interpreted in your php.ini.
Basically: remove the .txt from the end.

Purchased Web template. Index file is PHP instead of HTML, how do I edit the template?

I purchased a marketplace template. I am used to see index.html files when I load them into brackets for editing. This template however has an index.php file.
My question is how do I go about editing the index.php file? Would it be the same as I would with an index.html file? Brackets also does not read PHP and I would have to run it from a server with the host name url (something I also don't know how to do).
To execute php files, you have to use a local web server as Billy points out. On Windows and Mac you can use XAMPP to do so, on Linux you should prefer setting up a LAMP-Server (Tutorial).
After you installed your webserver you have to save your files under the server's document root. You can find it at the place where you installed XAMPP under the folder /htdocs (the place on Linux varies, on the latest Ubuntu flavors it should point to /var/www/html) You have to move all the files there. Now, you can open those files in your web browser by opening localhost/your-folder/index.php.
Edit your PHP files as you'd normally do in Brackets, it can handle PHP files. To use the Live Preview feature you have to go to File → Project Settings and enter your path with localhost here (don't add the file name, Brackets will do that for you) e.g. http://localhost/your-folder and you should be all set and running.
You can use live editor to edit your php file such as https://c9.io/ and its one installation is free and that's how you can upload your files: https://docs.c9.io/discuss/5582d455ea39a93900224238 . Just start custom php project and make it private.

my "phpinfo.php" file loads a completely blank page in both firefox and safari [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
Background: I want to learn PHP and in the tutorial that I'm doing, one of the first things it has you do after getting PHP and mySQL going is to to test it by making a phpinfo.php file and opening it.
Problem: In FireFox, the file loads a blank page. In Safari it only shows the code that I wrote: <?php phpinfo(); ?>.
Steps so far (not necessarily in order):
I followed the steps at http://coolestguidesontheplanet.com/how-to-install-php-mysql-apache-on-os-x-10-6/
I have made a phpinfo.php file using text editor in plain text mode (as opposed to rich text) and saved that in my "sites" folder. This is the only line in that file:
<?php phpinfo(); ?>
In system preferences, I have web sharing turned on and mySQL server running. I have removed the # from in front of the load php 5 module line in the httpd.conf file as per the tutorial I mentioned. I've tried restarting the web server using "sudo apachectl restart" and then restarting Firefox.
I looked for a php.ini file in my etc folder. There wasn't one but I did find a php.ini.default. I duplicated that file and renamed the duplicate to php.ini. I'm not sure this step was a correct step to take but from the little I could find on the internet it looks like somebody else has done this and I thought, why not?. (phpinfo.php loaded a blank page before and after this step)
I typed php -i | grep php.ini into terminal and got the following:
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /private/etc/php.ini
and a timezone warning
So I decided to look for a php.ini in the /private/etc folder and there is one there. ( The one I created was in the /etc folder not the /private/etc folder.)
I have searched the internet including this website for answers.
System info:
When typing php -v into terminal it tells me that I am using PHP 5.3.28
When typing mysql -v into terminal it tells me that I am using mySQL 5.6.10
I am running Mac OS 10.7.5 on a Macbook Pro.
FireFox version: 40.0.3
Safari version: 6.1.6
Questions:
1) Did I correctly create the php.ini file?
2) What am I missing? Why is the phpinfo file not loading in my browser?
3) Is the timezone warning important?
Make sure that your source code file has the .php extension in the end.
If you can see the code of your php file in your browser, probably your apache server is not started or it is not working properly. To set your development environment quickly, I recommend that you download an "all in one package" such as XAMPP server or WAMP server. It comes with everything that you need to get your php project working and it is very easy and quick to install.
Take a look at: https://www.apachefriends.org/index.html
Once you have installed the package, create a folder for your project and put your php file inside it. So, copy your project folder to "htdocs" folder, inside the WAMP or XAMPP path, open your browser and type the URL:
http://localhost/yourProjectFolder/phpinfo.php
You shall see your php file working on.
Good luck!

Call to PHP prints source, doesn't run when called from another file

I have a quick question that I can't figure out. I've tried searching Google and following examples, but I can't find anything.
I have an HTML form that I'm trying to process with a PHP file, but when I submit the form, it merely prints the source of the PHP file, it doesn't execute it. If I run the PHP file by itself (not indirectly through the HTML button), it works fine.
HTML form header:
<form id="registrationform" name="registrationform" method="post" action="processregistration.php">
Submit button:
<button type="submit" value="Submit" >Create</button>
The PHP form is just <?php print "Hello"; /?
Again, it runs fine if I just run the PHP file, but prints the PHP file (doesn't run) when it gets called through the HTML form.
Any help is appreciated.
edit-Running locally through Coda
edit-Here is the output that I'm getting:
Output when the PHP is called through an HTML action:
</php
print "Hello";
?>
Output when I run the PHP directly through Coda:
Hello
I had the same problem and just fixed it on my coda version 1.7.4
I installed MAMP on my mac and set up the apache path under MAMP preferences to the folder where my sites are located;
/Users/yourUserName/Sites
this points MAMP's 'http://localhost:8888' address to your sites folder, if you paste that on your browser you'll now see your sites folder's content in the browser.
then, all you do is point the local site's connection settings to the site you are testing;
I was going to post an image, but I'm new to the site and wasn't allowed.
Under my site's preferences i set up the addresses as follows;
Root URL : 'http://localhost:8888/yourSiteRootFolder/'
Local URL : 'http://localhost:8888/yourSiteRootFolder/'
Remote Root : /yourSiteRootFolder/
Local Root : /Users/amartinez/Sites/yourSiteRootFolder/
I hope this is related to the problem you're having and helps you fix it.
Line #19 from Coda 1.6 Release notes:
Coda no longer tries to locally
preview a remote PHP file while
editing/previewing
Listed under "Improvement" - doubt they brought it back for the 1.7 release.
I'm running Apache2/PHP5 10.5.8 OS X (no problem reproducing your issue with Coda)
Even when running my form.html and post.php files from /Library/WebServer/Documents folder.
My sample files work fine in the Apache env....I just needed to run them through Coda to "break" them. :-)
Are you sure the html file and the php script are on a server with php support enabled?
I've never used Coda, but you need to be running it through a server (e.g. Apache + PHP). You cannot just open the file itself within Windows.
Try looking at xampp as a quick server for testing.
If it works properly, you should be viewing the PHP file on something like http://localhost/test.php instead of file:///something/test.php.
This may be because you don't have PHP installed on your server. I would check to make sure that your hosting package included PHP, pre installed.
Here are some resources to get you started with that, if not installed:
http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml
http://www.php.net/manual/en/install.php
http://www.w3schools.com/PHP/php_install.asp
Edit: actually, I think I may have found it.
If you look at the starting PHP tag, you have a slash instead of a ?. If that's in your script, just change that to <?php. But it may not be, it could be that's just question format-ing.

Categories