I know a bit of HTML but I'm trying to learn PHP right now. Say I have a simple .php file like this:
<?php
?>
<!DOCTYPE html>
<html>
<body>
<h1>Title</h1>
</body>
</html>
If I'm on a mac, how do I get my browser to display the webpage represented by the HTML in this file? When I try open the PHP file it shows the actual HTML code, not the header. Apologies for the noobie question, I'm going through a PHP tutorial right now and the instructor did not elaborate on how to actually display the HTML inside the .php file so I'm a bit confused. He's running it through his localhost it seems.
So the deal with php, is that your browser can't run php code, that is done by the server. You'll need to get a local server to run it then. Since you're on a mac this should be alright:
https://www.mamp.info/en/
Once you've got that running, you'll need to put your php file into the 'htdocs' folder of the MAMP stack install. After that you can access it by navigating to localhost/filename.php
You need a local host to run your php script like MAMP for your mac. simply you can use free web hosting service from different online free host web server like Hostinger
You can't run a php file directly on your web browser that your are using.You need to have a local server host in-order to run your php file. Try downloading an using wamp server,Apache etc.
http://www.wampserver.com/
Related
I ran this in chrome browser but it didn't show the expected results...
It showed the exact same code which was written
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
To run PHP code in your browser, you need a server like XAMP, WAMP, etc. I recommend XAMPP as it is quite easy to use.
Download XAMPP from here: https://www.apachefriends.org/index.html
After the installation is done, go the the htdocs folder (C:\xampp\htdocs in windows) and paste your .php file there. After that, open XAMPP control panel and start Apache. Open http://localhost/test.php (assuming the filename is test.php) in your browser and see the result. Done!
Before working on PHP Codes. You should know how to run PHP codes in browser.
If you dont have any server then please install some server like XAMPP, WAMP, etc.
Also, If you have installed the server then might be the file you are running on browser has html extension. It should be a .php extension.
& should run using
http://localhost/test.php (If its in htdocs folder)
I suggest you to see the below documentation before working on PHP.
https://codeofaninja.com/2013/06/how-to-run-a-php-script.html
I am a beginner in PHP and I am trying to embed PHP in HTML as that is how I am going to need it in my project. This one is a very simple code saved as Trial.html
<html>
<head>
<title>Trial Page</title>
</head>
<body>
Hello, today is <?php echo "a great day!"; ?>.
</body>
</html>
When I load this page from server (I am running Apache Tomcat 7 locally), Only the HTML part is getting displayed and the PHP part is not. What am I missing? Do I need to include anything else or am I taking a completely wrong approach for this?
Rename "Trial.html" to "Trial.php" this is essential when you try to run php code.
You should save your file as .php and run it on a server with php installed on it.
check XAMP to install a local server
EDIT
Just saw that you're running Tomcat, just make sure php is installed, that your server is running, you're running your file from the server And your file is save as .php
You change file name from trial.html to trial.php
I'm trying to learn PHP through codecademy.org and I can't understand why the following code, when saved in as helloworld.php, returns no text. I tried in Internet Explorer and in FireFox, but nothing. I also opened the .php file in notepad and it looks fine, uncorrupted. What's going on here? I followed codecademy's instructions exactly, and the code works in the codecademy previewer. Code:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p><?php echo "Hello World!"; ?></p>
</body>
</html>
Have you put the file on a server with PHP? The code has to be executed by PHP interpreter, and the browser itself doesn't have one.
The easiest solution is probably setting up a server locally, on your computer. I recommend XAMPP (for Windows), it's easy to install and use.
you need a server (apache, php, ...) to run php. look at zend server, xampp, ... its quite easy to setup.
You need a webserver parsing your php-file. Try to use XAMPP.
You should have a web server and php installed in your computer to get it run. Try wamp server: http://wampserver.com/ It is a very easy to use solution for windows. You just install it and it handles everything else by itself!
If you already have a server installed, make sure you put the files on the "www" directory!
you should put your code into php file make helloworld.php file and put in wamp www folder and run with wamp server.
for hello world program in php just follow this tutorial
but before that you should have wamp server or xamp server on your pc(wamp/xmap are open source software's and easily can found on google)
your 1st php program code:-
<?php
echo "hello world";
?>
output:-
hello world
Helping Step by Step Tutorial :https://www.youtube.com/watch?v=7qtqzhdEX-c
after this tutorial you can able to move other intermediate level programs of php.
I have to do a php project. I done php before so I understand the syntax for the most part. Just for a test, I made a file.php and in it I wrote:
<html>
<body>
<?php echo "helloWorld"; ?>
</body>
</html>
Well it won't display. The screen is blank. I tried it in chrome, firefox, IE and nothing wants to dispaly. Actually in IE, the source is displayed which is wierd. I also tried it without all the html and just used xampp to render it. It will not work. If I right click tho in the browser and view source, the code is there. Any ideas on what's going on?
Well, it comes from your web server configuration. If you're using Apache, have you enabled the mod-php module?
If you're new to setting up your own server, i would recommend using XAMPP (or WAMP), these are preconfigured PHP, Apache and MySQL servers.
If you're sure you have setup your server correctly check the following:
Make sure your executing your files from the server directory and NOT from a local directory. (your URL should look something like "http://localhost/test.php")
Note: You will need to phisically store the files in a place the apache server will look for, an example from XAMPP (on Windows, as thats what im assuming your using) is: "C:\xampp\htdocs"
Make sure your file ends in .php or something else that the Apache server will pickup as a PHP file. (.php3, .php4, etc)(make sure you didn't accidentally leave a .txt or something like that at the very end)
Check mod-php module is enabled (as Julien mentioned)
Hope that helps!
Edit:
Try
<?php
phpinfo();
?>
That as well, it should give you the php configuration information if the server is setup correctly.
EDIT2:
I see that you are using XAMPP, double check that the following file exists at the very least:
"C:\xampp\apache\conf\extra\httpd-xampp.conf", it loads the PHP module
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.