Reputation: 35
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");
Upvotes: 1
Views: 87
Reputation: 406
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
Upvotes: 0
Reputation: 23379
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:
Upvotes: 2
Reputation: 13313
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
Upvotes: 3