Html PHP combining - php

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

Related

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.

How to call a php function in HTML

Rookie here, so please correct me if I have anything wrong.
So here's a snippet of my HTML:
<html><body>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
</body></html>
The function, $fgmembersite->UserFullName(), returns a string (100%, if I call the function within tags it prints out correctly). How do I get it to echo out in the HTML?
I think an alternative would be to echo the entire HTML code, and I think it'd work then, but I don't want to do it that way because I read somewhere that echoing all of your HTML is bad. Could somebody also confirm/deny that?
Right now, this is what shows on the site:
Welcome back UserFullName(); ?>!
No idea why
Thanks for your time!
I think you want the code to look like this...
Welcome back <?php echo $fgmembersite->UserFullName(); ?>!
This will ECHO the result of the function call.
What you are seeing is the result of the browser trying to parse the PHP. It is trying to treat <?= $fgmembersite-> as an unknown tag and then renders the rest as text.
There are two possible reasons for this:
You need to pass it through a PHP engine first.
You need to access the file through a web server which supports PHP and is configured to run your file through PHP when it is requested (typically this is done by using a .php file extension).
Note that installing a web server then double clicking a PHP file in your file manager isn't sufficient - the browser will just load the file from the file system. You need to type http://etc etc.
It is also possible that you have short_open_tag disabled (which is common and sensible) and are using PHP 5.3 or older (which isn't a great idea, 5.3 is still supported but it is the oldest branch that is).
If this is the case, your options are:
Upgrade PHP
Use <?php echo ... ?> instead of <?= ... ?>
Enable short_open_tag
Can't you just do
<?php echo $fgmembersite->UserFullName(); ?>
?
use it as follows:
<html><body>
<h2>Home Page</h2>
Welcome back <?php echo $fgmembersite->UserFullName(); ?>!
</body></html>
Do you have short_open_tag enabled on your server? You need to have that enabled to use that syntax. If you don't have that, use <?php echo $fgmembersite->UserFullName(); ?> like the other users suggested.
You can check the setting on you server by creating a file containing <?php phpinfo(); in it, and accessing that.
Also, echoing all your HTML inside the <?php tags are considered a bad practice because you should separate your business logic and your HTML. Echoing HTML directly from inside the PHP tags quickly leads to a mix of HTML and PHP, and it will be harder to read (for others, and yourself later on)

Using <?php ?> VS. <? ?> [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are PHP short tags acceptable to use?
We are currently using a mix of both styles for writing php code inside our phtml files. Nothing seems to be breaking but I'm just curious about using one over another.
(looking for something more then.. 'its best practice' answer)
<? ?> are called short tags, they need to be enabled on the server in the php ini file. The only reason why you should not use them is because of compatibility, which in my opinion is not that big a deal, but if you are writing a CMS that is supposed to be used by other people on other servers you should use normal tags <?php ?>
Always available:
<?php & ?> (Most Common)
<script language="php"> & </script> (Least Common)
Enabled by short_open_tags php.ini directive:
<? & ?> (Short Tags) allows for <?=$var;?> so you can echo.
<% & %> (ASP Style)
When I release code, I always use the <?php & ?> as it's supported across the whole ecosystem of PHP. If you use the short open tags and you distribute the code, then there is a chance that some clients will not be able to use it.
The <? ?> syntax needs to be set to "on" in php.ini, so it may not work
<?php ?> always works.
<?=$var?> is the only sensible way to output a variable if you're using PHP templates. Writing each variable on the page as <?php echo $var ?> is a pain. So, if you're a masochist (or using different template), go for the long form, otherwise stick with short (at least in templates)
<? is also used for other script languages - if you have more than one installed on the server, it can lead to problems.
<? is disabled in some server configurations (short_open_tags in the php.ini), so if your script should run on other servers, use always <?php
<?php /* .. */ ?> is canonical.
<? /* .. */ ?> is "allowed" but certainly not recommended. This feature is enabled by setting short_open_tags to true in your php.ini, which hasn't been default for a while.
The rationale is out of scope of this question; there's plenty of documentation out there.
In particular, see this question.
<? must no longer be used as explained here.

Which PHP tags are always available?

Of the 4 types of PHP tags:
Standard tag : <?php ...?>
Short tag : <? .... ?>
Script tag: <script language=“php”>
... </script>
ASP tag : <% ... %>
Which is/are always available ?
The standard <?php and the <script> tags are always available.
The two others depend on configuration settings.
From the reference:
There are four different pairs of opening and closing tags which can be used in PHP. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
Short tags are only available when they are enabled via the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option.
ASP style tags are only available when they are enabled via the asp_tags php.ini configuration file directive.
You should always use the PHP tags <?php and ?>.
These tags will always be available on a PHP server, whereas the shorthand tags (<? + ?>) can be turned on and off in the php.ini file, I generally go with <?php ?> as this increases portability.
so far I know PHP's short opening tag is going to be completed deprecated from PHP 6. So it is beast to use .
You have to use <?php tag. You can use <? tag when the "short open tag" property is "On" in your php.ini file.

php syntax issue?

value="<?=$file_source?>"> it will show the >"> at my browser
but if
value="<?php echo $file_source?>"> it will show correctly in browser.
Is it due to php versioning?
Also I realize some php classes written by others (which I download online), doesn't use <?php ?> , it directly use <? ?>
But when I run it locally at xampp, I need to code it as in order to get the correct output.
<?= and <? are PHP short tags. They don't work because it is not enabled in your PHP config, and it is generally recommended to avoid using them and always use <?php
See Are PHP short tags acceptable to use?
PHP has a short_open_tag directive in php.ini which needs to be on for <? ... ?> and <?= ... ?> to work. However, the recommendation is to only use <?php ... ?> in your code.
So, for your code to work you will need to either turn short_open_tag on, or change the code to use <?php ... ?>.
Short tags (<?) have been deprecated.
You need to enable short_open_tag in your PHP configuration. People tend to think this a bad idea, although the short echo sytax is very useful.
This directive also affects the
shorthand <?= , which is identical to
<? echo . Use of this shortcut
requires short_open_tag to be on.
http://php.net/manual/en/ini.core.php
It seems that short_tag_open is disabled and thus any other PHP open tags except <?php are ignored.
What you can also do, if you're not sure about the php.ini, create .htaccess file.
From notepad, save the file and called it '.htaccess'
In that file, put short_open_tag On

Categories