PHP inside HTML doesn't work - php

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.

Related

Echo Class Inside of HTML Tag with PHP

Trying to get this piece of code to run:
<li><a <? if ($tab=='Home') echo 'class="activetab"' ?> href="/">Home</a></li>
Right now, my PHP/Apache on EC2 setup spits out
href="/">Home
It looks like the php inside the tag is messing up the rendering, and I'd like the tab to be rendered.
The site is hosted somewhere else and works, so it seems like a configuration issue with either php or apache.
Check your php short tag is open or not?
In your php.ini change the short_open_tag = Off to short_open_tag = On.
<? ... ?> – This is called as PHP short tags which will work based on the value set with short_open_tag directive of PHP configuration file.
Otherwise use <?php ... ?> as delimiters which are also preferable, since, PHP code enclosed with in <?php ... ?> could be recognized with other PHP servers independent of the tag related configuration directives.
Try By This Code
<li><a <?php if ($tab=='Home') echo 'class="activetab"' ?> href="/">Home</a></li>

Html PHP combining

I'm new to programming and I'm trying to combine html and php codes.
Such as
<table>
<tr>
<th>Header</th>
</tr>
<tr>
<td>DATA goes here : <?php echo something_number("$total");?> </td>
</tr>
</table>
Is there other methods to declare php in a html line???
thanks for the help by the ways.
<?php
include ('db.php');
set_time_limit(0); // set unlimited execution time
ini_set("memory_limit","1000M");
Yes you can also use short echo tag <?= ... ?>
<td>DATA goes here : <?=something_number("$total");?> </td>
You can use this tag to print something. But for program logics, you should use <?php ... ?> tag. You can read more about PHP tags here.
Note: (From the perspective of Anant) Whenever you have PHP code inside HTML. The file extension should be .php and not .html
If you are running your PHP in files that have an HTML extension, and you're using Apache, you can add this to the server configuration or (if the server is configured to allow it) create a file called .htaccess in your webroot and add this line:
AddType application/x-httpd-php .html
That will allow your php to run in an .html file.
Depending on which version of PHP you're using and how it's configured, there may be several available substitutes for the usual <?php ?>.
If you're using PHP<7 you can also use <script language="php"> </script> without any configuration changes.
If shorttags is enabled, you can use <? ?>, but this is not usually turned on by default in an out of the box *AMP stack.
ASP style tags aren't often used or recommended, but with proper config you can also use <% %>
This is also supported out of the box for outputting inline, not for running code<?= ?>. This is shorthand for <?php echo ...; ?>
If ASP style tags are enabled you can use this shorthand for outputting <%= %>.
Generally, for compatibility and convenience, I would recommend sticking to <?php and ?>.
For some good info read:
Escaping from HTML
PHP Tags
Running PHP from other file types
I know that you have got your answer and yes you should use <?php ...... ?> And also you can use short hand notation too, but I would not recommend that.
And you have to have .PHP extension in your file to use PHP methods and write PHP code.
Also you can put html code as well in .PHP files.
Thanks

Embed PHP into HTML

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.

Displaying Php echo message in an HTML page

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...

Extra output in for php in html hello world file

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

Categories