PHP syntax not working in localhost - php

I was trying to execute this piece of code
<?php
$number1=10;
echo number1;
?>
I am using XAMPP Server and notepad++ as the editor. I have saved the file as firstfile2.php in the htdocs folder that is stored in D drive. When i am running this file i am getting this code as the output and not 10.
Can anyone explain what is wrong?

In the browser you need to navigate to http://localhost/firstfile2.php
Make sure your file is not actually called firstfile2.php.txt because you didn't save it correctly
Make sure you have full php tags( short open tag <? is not enabled by default in some servers)

Related

If I rename an index.html to index.php, the CSS style disappears

Yesterday I discovered a website called Too Plate:
https://www.tooplate.com/about
It has free HTML templates, and all they are very beautiful.
I had the idea to turn the theme that I chose to PHP, so, I just renamed the file index.html to: index.php
But, when I open it using a brwser, all the style is gone... As if the template could not find the CSS file... But I just renamed the file, I did'nt change anything inside the code (I only added the "" tags a the beginning...)
This is the template:
https://www.tooplate.com/view/2122-nano-folio
How can I fix this, please?
Once you add the PHP extension it handles the file as a PHP script, which is a server-side language that's get executed on the server before loading the page, so to view ant PHP file you have to have a server running the PHP code, but HTML runs on any browser.
So what happened was that because you changed the extension the browser treated it as a PHP script and cant Execute the script because it's not a server.
you must need to run the code in server eg: local servr xampp or wampp then only your php code work try this
https://felbdogg.com/test2/index.php
code works fine... I'm not sure what you changed but I uploaded it to my server and works the same as what I downloaded.
If your opening it on your computer without a server it will not load. XAMPP is free and works great for testing.

Display html page in php file

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/

New php user--trying to open .php file in test environment

Using mac OS, installed MAMP. Everything seems to be running fine. Created a PHP file in the htdocs folder, tried to open it with Firefox but won't recognize PHP file. Asks me which program I want to use to open the file, when I choose firefox, blank screen comes up.
Open it by typing in your browser localhost/path/to/your/file.php and MAMP (assuming everything is in fact working) will then parse the file and return the output from the request.
For Mac & MAMP, the path in the address bar should be:
http://localhost:8888/folder_name/your_page_name.php
Also, this will help you configure MAMP appropriately
http://sawmac.com/mamp/
inspite of all bad configuration of mamp,opening file with firefox must show something written over it,so please show code u wrote there as said by Dan and DarkXphenomenon

local php files will not work

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

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