my .PHP file doesnt seem to work - php

i have a rather simple .PHP file with the following code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>hello world</p>
<?php echo "test";
?>
</body>
</html>
yet when i run it all i see is "hello world", i checked firebug and it showed me this
and in chrome it simply comments the php out
am i missing something important here?

PHP is an pre-processed, interpreted language which means a web server (like apache) with the PHP interpreter installed must serve it to you. You must install a webserver on your local machine and access it via a URL like http://localhost. PHP cannot be viewed the same way as locally-served HTML files (like what happens when you double click them) through the file:// prototcol.
But never fear, it is not hard to get a webserver installed. Try something like XAMPP, which will install Apache, PHP, and MySQL for you and give you a nifty little control panel.

You seem to be missing PHP. Where is the file located? Are you trying this in a server?
If you are new to PHP I recommend you using WAMP or LAMP, it's the easiest way to start using the languague.

A quote from here PHP gets commented out by the browser
Browsers shouldn't comment out PHP, they shouldn't even see PHP.
Most likely, what you are experiencing is that the <? is not being
parsed by the PHP engine and being delivered to the browser. The
browser is then treating it as an unknown tag and ignoring it.
The solutions are:
Don't use short tags as they are not turned on for all PHP installations. If you have > <? some code change it to <?php some code
2.Make sure you are trying to run your PHP on a server that supports PHP.
Try installing XAAMP, It will install everything you need for PHP Development.
Oh! I think you're saving it as .PHP with capital letters, Don't use capital letters, Use small letters, so your file name should look something like test.php.

Related

PHP implementation into HTML

I'm currently beginning with PHP and I wanted to get started with a mini-project for some hands-on experience. Here's the code that I began with:
<html>
<header>
<title>Test</title>
</header>
<body>
<h3>Some example title</h3>
<?php
$randomNumber = rand(1000,9000);
print "Here's a random number: {$randomNumber}.";
?>
<p>Some more text.</p>
</body>
</html>
However, when I saved it as index.php and previewed it on Google Chrome, the PHP part doesn't show up. Can anyone help me realize what I'm doing wrong?
Thanks!
To run PHP code (server side) you need a server, or a 'server-emulator' some of the most commonly used are:
XAMPP
WAMP
Unlike the html in your .php files, the php code must be processed by a server and returned to the browser. I've tested your code with a server and it works like a charm.
After installing one of the above "emulators" you need to (this is a simplified tutorial, check a real tutorial):
place your .php files in the public_html or htdocs folders that are
created in the emulator's folder in your computer.
Turn on your server
go to localhost/your-file-path/index.php
If you only need to test soft code (like what's on yout example) and not develop whole projects use IDONE
PHP is used in the server side, so, you need to install an environment (Apache, PHP) to interpret the PHP. If you use Mac or Windows, you can install Mamp. For unix, you need to install some packages, i'll invite you to see this tutorial: Lamp tutorial.

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/

How to get Embedded PHP working in 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

php server doesn't seem to execute code, although server is running

I haven't worked in a very long time with php so this question may be a very noob one.
I've installed apache server 2.0.65, and although when i call the localhost, my page in htdocs gets executed, only the html code appears in the view. The php code appears in the source view as html would, thus leading me to believe that it doesn't get interpreted.
My code is the from w3school:
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
what am i not doing ok?
You did not install PHP
You did not configure PHP to run your file (by default it will process files with a .php extensions)
You aren't actually visiting http://localhost/foo.php in your browser
Apache would just run HTML, CSS. In order to make PHP work, you must install PHP, and set environment variable on your local machine, so that all files with .php extension would be run by PHP.
Filename must have a .php file extension.
PHP must be installed and running - if you can open a terminal and type php -v or use top to see if php is running.
If you are running on your local computer try installing WAMP or something similar.

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

Categories