I'm trying to use PHP to print out values for my select statements but I can't get it to work. I've even tried just to print something out in my html with php, but i can't even seem to do that. Can anyone tell me what's wrong with this
<html>
<head></head>
<body>
<ul>
<?php echo "register Student:"; ?>
</ul>
</body>
</html>
Make sure you save your file as .php not .html.
myfile.php:
<html>
<head></head>
<body>
<ul>
<?php echo "register Student:"; ?>
</ul>
</body>
</html>
You will also need to run this file on a server that has php installed. It won't run directly in the browser like an .html file will.
Keep these things in mind.
Your file name should always end with .php extension not .htm or .html
You should always run file on localhost/ where you set up your environment
Please follow the standard of W3C Use a "li" in you "ul"
Hope it works.
Name your file whatever.php
Make sure it is NOT whatever.html
If that doesn't work, be sure to check whether or not you installed php. I suggest a local testing ground such as XAMPP. If you don't want to do that, upload it to a free or paid hosting service.
Related
I just want to know why does this HTML file shows nothing:
I'm running it on a web server
Name of the file is index.php
I've fixed the <? issue but it still doesn't work
Here's the code:
<html>
<head>
</head>
<body>
<?php
echo "hdfguhbgzusgdfghdhhfgh";
?>
</body>
</html>
You should make sure that following are given:
PHP on your server
Files have to end with ".php"
Use open Tag <?php and not <?
Then it should work.
For a definite solution you should provide further information.
As edited by halfer on Apr 8 '14 at 14:13, the showing code had no <? problem anymore. The code seemed well written.
In case those code still do not work, perhaps people must add:
AddHandler application/x-httpd-php .html
inside the .htaccess file.
The .htaccess file must be in the same directory with your html file. If you can't found the .htaccess file although you already turning on the show hidden file option, you can create new .htaccess file and include AddHandler mentioned above in the file.
Just create a blank text file and name it with .htaccess on the mentioned directory.
you missed php after <?
so, change:
<? echo "hdfguhbgzusgdfghdhhfgh"; ?>
with
<?php echo "hdfguhbgzusgdfghdhhfgh"; ?>
Some tips:
If you dont see the text, try to open source code in browser.
Make sure, that your file is .php and not .html
Use <?php as opening tag instead <?
If you want to just echo text, you can use <?echo "YourText"; ?>
Everything else seems OK
The short_tags comes disabled by default, so you have to use
<?php
echo 'Teste';
?>
invés de:
<?
echo 'Teste';
?>
To enable the option to just turn short_tag in php.ini
http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
PS: For performance reasons it is recommend to use "(double quotes) only when the echo is variable, so that PHP will parse and find the variable If you place." (Double quotes) there is no variable in the php echo to atoa parsing, try waste of charge.
Hi I am a newbie to web development. I am trying to display echo message from a php file in html page.
PHP File:
<?php
echo "Hello";
?>
Other file:
<form method="get" action="latest.php">
</form>
Note: These both are two different files
I am completely new to this. I don't know where I am going wrong.
Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP. However you can simply tell your web server to parse your HTML files as PHP too and then you can run your PHP code in an HTML file wherever you want. Now assuming your Web server is Apache.
Step 1:
Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website
AddType application/x-httpd-php .html
Step 2:
Go to Your HTML file and add an include statement where you want your PHP file to be included
<form method="get" action="latest.php">
<?php include ("yourPHPfile.php"); ?>
</form>
Step 3:
All done, now go check output in browser.
Just put the code together:
<form method="get" action="latest.php">
<?php echo "Hello"; ?>
</form>
Everything inside the Tags will be interpreted as PHP, no matter where it is. But make sure the ending of the file is .php!
May be try this..
<form method="get" action="latest.php">
<?php include('php_file.php'); ?>
</form>
Please note that other file should also be a .php file. if it is an html file you either need to change it to .php or you need to make ajax request.
You can't execute php codes in a file ending other than .php.
But you can have HTML code displaying in a .php file.
So I would recommend you change the extension to .php.
You can simply have
<?php
$name = "Harry Potter";
?>
<h1>Hello, <?php echo $name;?>!</h1>
Also you need a web server software and php processor. WAMP or XAMP may help you on that.
I am editing my answer since some are really picky.
In general you can't execute php in files which dont have the .php extension. But with some tweaks and line changes you can. I am just stating that you can't since, you mentioned that you are new to web development and I would like to make things simpler rather than confusing
If you want to execute php code in .html file extension than u need to do some htaccess changes.since file extension plays a major role ,it only tells the browser how to handle or parse a particular page.
You can create a .htaccess file at the root folder of your website and in the htaccess file add this line:
AddType application/x-httpd-php .html .htm
If you only plan on including the PHP on one page, it is better to setup this way:
<Files yourwebpage.html>
AddType application/x-httpd-php .html
</Files>
This code will only make the PHP executable on the yourpage.html file, and not on all of your html pages.
If you are ok with changing your file extension to .php than your work will be quite easy.you can simply include a php code between html code.like this
<form method="get" action="latest.php">
<labe><?php echo "hello" ?></label>
</form>
Its so simple you can just put php tag and write php code even inside the html
<form method="get" action="latest.php">
<div><?php echo "Hello";?></div>
</form>
Whenever you echo something, it is printed on html page only.
If you want to echo inside a particular html element, just put the code there. For example,
<html>
<body>
<div class="myDiv1"><?php echo "This is myDiv1"; ?><br></div>
<div class="myDiv2"><?php echo "This is myDiv2"; ?></div>
</body>
</html>
Hope this helps...
I just learned how to include php .Here's the index or main php file
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php include 'header.php'; ?>
</body>
</html>
now in header.php file which way is better to print html
Way 1 directly use html without php
<header>
<h1>Header</h1>
</header>
Way 2 Using php and echo
<?php
echo '
<header>
<h1>Header</h1>
</header>
'
?>
Another quick question. Will it work if I use .html for the base or index file ??
sorry for my bad english
Directly use HTML without PHP:
<header>
<h1>Header</h1>
</header>
As for your second question:
The file you're using include() in must have .php extension, but the file that's being included doesn't necessarily need the .php extension. The .html extension would work fine as well.
Just include the PHP file.
BTW, in case you haven't, read about this too:
Difference between require, include and require_once?
HTH.
you should use include_once ;-)
http://us2.php.net/manual/en/function.include-once.php
There are a few ways to go around it. Rather than echoing everything, I like to go like this:
<title><?php echo $title; ?></title>
Or if I have a nice block to work with, maybe within an if statement, I also like to go like this :
<?php if($weather = "sunny") { ?>
<div id="sunny">
<p>It's a beautiful day outside.</p>
</div>
<?php } // end if($weather = "sunny")
else { ?>
<div id="sunny">
<p>Today is yucky.</p>
</div>
<?php } ?>
The answer is sometimes different. If you use the same header on a lot of pages, use Way1. If you're only doing this in one place, you want to use Way 2 (or keep it in html), since it's more readable at quick glance to someone studying the page.
You can use .html for the file if you change the server config to tell it to look at it for php first, but you shouldn't do that, it just increases complexity and may have unintended consequences if you do it wrong.
Use .php or .phtml for files with any amount php in it. The only distinction you can make is to use .phtml files for files that are mostly html.
I'm creating a LAMP stack and just started learning PHP. I'm having trouble with my Hello World program. On my server I have a file called index.html. This is the contents of that file (which is basically copy pasted from an online guide):
<html>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
echo '<p>Hello World</p>';
?>
<b>Here is some more HTML</b>
</body>
</html>
When opened on a web browser, I expect my output to look something like this:
My Example
Hello World
Here is some more HTML
Instead, it looks like this:
My Example
Hello World
'; ?> Here is some more HTML
Why is that extra "'; ?>" there? I'm probably making a simple mistake. I've tried accessing index.html on chrome, firefox, and safari, same result each time.
PHP does not work in HTML files, try renaming it to index.php
Apart from that the code is fine (maybe some best practices like separating HTML and PHP, but you'll get to that later)
The reason you don't see the whole PHP code on your screen is because the browser is trying to parse the element <?php *** ?> as if it where valid HTML (like <span>). If you check the html-source, you will see all of your code.
As someone in the comments below mentioned, there are ways to make PHP work in HTML. You should not do this, unless you are very aware of what you are doing. It is not a default setting and it should stay that way. If you should be able to use PHP in HTML files, .phpfiles would not have to exist.
When you expand your knowledge, you will start separating HTML files from PHP files and make PHP include templates. Two separate files for two separate goals
Rename the file to index.php.
In addition to this, the PHP code must be interpreted by a WEB SERVER(Apache in your case).
For example(if you are using XAMPP on Windows), and your php file is present in the webcontent directory of your server's document root(htdocs), then type the following in your browser
http://localhost/htdocs/webcontent/index.php.
Like others said, you must rename index.html to index.php, but if you must use index.html, put the line below into your .htaccess file, so you can process .html files as .php.
AddType application/x-httpd-php .html
I would like to learn PHP and started reading this info's in the website: And I have question about this code:
Why is this line not displaying? " echo 'Neo: I am Neo, but my people call me The One.';"
Thanks
<html>
<head></head>
<body>
Agent: So who do you think you are, anyhow?
<br />
<?php
// print output
echo 'Neo: I am Neo, but my people call me The One.';
?>
</body>
</html>
You need a webserver, and give it a .php extension.
You cannot run it directly from your drive (e.g. /home/user/file.php or C:\file.php), you must run it from your server (e.g. http://localhost/file.php or http://example.com/file.php)
A webserver can be downloaded from http://www.xampp.org/
Make sure you are using a virtual webserver such as mamp or xampp,
Also try print instead of echo with "