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.
Related
i want to make an image gallery for my website that pulls jpg files from a directory, so i don't have to individually write <img> tags for all 100+ images. a php gallery seems to be the only way to go (though i'm open to other suggestions that might involve pure jquery/ajax, if that's possible!)
i have a very preliminary understanding of php. i've installed php 7 and apache 2.4, they're currently in the root directory of my computer. i was able to create a very basic 'hello world' index.html with a generated php image, which is also saved in the root directory at C:/Apache24/htdocs and hosted at http://localhost:
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h1>Hello, PHP!</h1>\n";
?>
<img src="myimage.php" alt="Image created by a PHP script" width="200"
height="80">
</body>
</html>
however, all of my website files are located in C:/Documents/Github/xxxx/xxxx, which is where i usually push all of my commits from. i use jekyll as a server to test my site offline but there's no liquid code involved (it's a one pager written all in HTML and is hosted at http://localhost:4000)
this is how it looks when i copy the code from the 'hello world' index.html to my website index.html:
so i am wondering how to execute php on my website, which is hosted on github. i have a basic understanding of how it works locally at my root directory, but i'm not sure how to execute it in my local/remote github repos.
do i need to move the php folder? edit the .ini or httpd.conf files? install a plugin?
thanks!
EDIT: so i was able to get the php script to execute locally, but when i pushed my changes to github, the script wasn't executed?
LOCAL WEBSITE
REMOTE WEBSITE
okay, i may have figured this out.
i deleted the individual php and apache folders, and instead used wamp as my server instead of jekyll.
i then relocated my github repository to the wamp/www directory, and the php script is now executable on the local index.html file. i think a php interpreter is what might have been missing.
i am still having issues with getting the script to work on the live website.
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/
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.
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 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.