is php embedded in html or is html embedded in php? - php

I have read on many sites and documents saying like php is embedded in html.
This is ok to understand but this statement is bit confusing for me.
If i have .html file and if i used php code for eg. following line:
<h1> <?php echo "This is PHP"; ?> </h1>
It wont work. But if i used same line of code in .php file it outputs the result.
So my confusion is if we are putting php code in .html file it is not giving results but still we are saying php is embedded in html.
Why cant we say html embedded in php and not php is embedded in html?
Now this line also outputs the same if it is used in .php file
<?php echo "<h1> This is PHP </h1>"; ?>
<h1> <?php echo "This is PHP "; ?> </h1>
Now here the file is .php so we can say we are putting html code in .php file, so if i say html is embedded in php is it right or wrong?

I would say this distinction is not right and not wrong, it's just useless...
You write a .php file, with some php code and some html inside.
Web server parses your file, interprets and executes php code, combines it with html, and produces an html page, which is sent to the requesting browser.
Thant's it... :-)

It depends on your server configurations if you want .html files to be treated as an php file(if it not already does) add this on your .htaccess file
<FilesMatch "\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
Now these is an example of PHP in HTML
<input type="text" name="<?php echo $name;?>" value="<?php echo $value;?>"/>
And this is the example of HTML inside PHP
<?php echo "<input type=\"text\" name=\"$name\" value=\"$value\"/>";
You can not say html is embedded in php only if it is inside .php file it depends on your code and not your file extension .

Related

Making an exception for characters such as '<= $example ?>' in the HAML pre-processor?

is it possible to make the HAML pre-proccessor ignore for I.E. <?= $test ?> and just print it out as it is? To then turn the HTML file into a PHP file to be used as a template.
If using code in page with .html extension then it will directly shown the PHP code as it is.
But for .php file, htmlentities can be used. For example:
echo htmlentities('<?= $test ?>');

Why won't my PHP parse correctly?

I know the title is vague, but the details for this particular question would make the title absurd.
I have an index.php file that has a virtual include within it:
<!--#include virtual="/includes/header.include"-->
This header.include file is HTML and I want to add a few lines of PHP inside of it.
My .htaccess file is set up to process .include files as php. The actual line from the .htaccess file is:
AddType application/x-httpd-php .include
The PHP that I'm trying to add is:
<?php echo "test <br />"; ?>
But when I load the website, the only thing that prints is:
"; ?>
If I view the source of the index.php I can see the entire PHP code that I have above.
Some other things I've already tried:
Copy the .htaccess file into the directory that the header.include file is in
Both the accepted answer and the answer with 6 upvotes from this question
Tried <br> instead of <br />
What am I doing wrong? Why won't the PHP get parsed correctly?
My PHP version is 5.1.6

PHP inside HTML doesn't work

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.

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