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.
Related
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)
Are there any caveats one should be aware of when using the alternative PHP tags:
<script language="php">
// ...
</script>
I've tested in 5.2.X through 5.4.X with no issues, however I cannot find exhaustive information on the topic. My greatest concern is sudden deprecation. Any information about support for this alternative would be great.
To answer those asking "Why the heck do you want to use that anyways?", I'm using PHP in XML files, where the surrounding XML acts as meta-data to the script contained therein. Unfortunately, XSD cannot validate processing instruction nodes, er go, using an element tag to wrap the PHP would simplify the validation process. This would keep the semantic value.
As it stands I need to validate against the XSD, and perform a post-validation sweep using XPath to check for processing instruction nodes.
According to the PHP documentation:
While the tags seen in examples one [ <?php ?> ] and two [ <script> ... </script> ]are both always available, example one is the most commonly used, and recommended, of the two.
So no, it probably will not be deprecated, although <?php ?> seems to be recommended.
The commonly known alternative PHP tags (ASP tags <% %>, <%=, and the script tag <script language="php"> and their respective directives) were proposed and accepted to be removed starting version 7.0.
The only help offered nowadays is a just a porting script to replace them. You may wanna check it to keep your code up-to-date.
There's also a short tag available just for displaying values. It only saves you the explicit declaration of echo and printing functions. Before version 5.4.0 it needs to be enabled via short_open_tag=On on the php.ini file.
<?= $previouslyDeclaredValue ?>
Smarty and Laravel blade are examples of template engines that enable the use of predefined and custom tags as alternatives that depend upon the installation of its corresponding platforms. Their core purpose is template design.
I am building my website completely in PHP. I am trying to make it as much flexible as possible.
I have seen there are some softwares made in PHP that are able to get a HTML page, and before showing it, the PHP code recognizes the code inside brackets {PHP Code} as PHP code, runs it and only then shows the final page.
<h1>Hi My Name is {echo $name}</h1>
How can I achieve the same? I know there is Smarty Code. But I do not want to learn Smarty, I just want to know how to check a HTML page with PHP, find every bracket and threat that as PHP before showing the page..?
Can you point me somewhere?
Are you looking for PHP's basic syntax?
If you enable short_open_tags (it usually is enabled by default), this will work:
<h1>Hi My Name is <?=$name?></h1>
otherwise, this will always work:
<h1>Hi My Name is <?php echo $name; ?></h1>
PHP is already a templating language - there often is no need to add another layer of templating on top of it.
I want to keep the template files separated from the php engine
In fact, you don't
Your template files would behave as native PHP files in every way.
So, there is asolutely no [logical] reason to prefer such a strange solution over native PHP.
use the php tags for the echo statement.
<h1>Hi my name is <?php echo $name; ?></h1>
Well, just point apache to index.php which includes phtml templates into itself. Use <?php ?> instead of { }.
I usually create modular websites, each part of the website being a .php file which will be included in the main pages.
Is it "better" to output HTML within PHP files using echo or to close each time the php tag ?> and open it each time I need to access a PHP function/variable.
V1:
<?php
$v1=$_POST['name'];
echo "Your name is".$v1;
echo $v1." if you want, you can log out";
?>
V2:
<?php $v1=$_POST['name']; ?>
Your name is <?php echo $v1; ?>
<?php echo $v1;?> if you want, you can log out
The thing is that between the php tags there's much more HTML code (echoed) than actual PHP.
Does it affect the script performance if I close the tags each time? And is it safe to acces variables declared in a previous block of php code?
EDIT1:
When closing the php tags isn't the server clearing some cache for that script, or something like that?
I think you can select whatever you want, but you should use it everywhere. For myself, second one is better
Definitely v2. Plus , you additionally should read this one : http://codeangel.org/articles/simple-php-template-engine.html (archive link: http://archive.is/CiHhD).
Using V2 would be better as it wouldn't break the syntax highlighting or code completion in many IDEs, but both of them are as good as the other.
As far as I know, there is no (considerable) difference in performance.
You could also consider using a template engine, however, that does impact performance. The most popular template engine is Smarty, but there are others (some better, some worse) out there.
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