php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48848 Neater insertion of PHP variables in HTML
Submitted: 2009-07-08 08:02 UTC Modified: 2009-07-08 10:47 UTC
From: tcjohans at riseup dot net Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.10 OS: Windows Vista
Private report: No CVE-ID: None
 [2009-07-08 08:02 UTC] tcjohans at riseup dot net
Description:
------------
Hello,

Here is an idea of how to facilitate the interaction between PHP and HTML code. Much of this interaction just has to do with the insertion of the value of a previously defined PHP variable into the HTML document. Currently, this is done through a rather roundabout coding:

<?php echo $somevariable; ?>

This snippet contains 28 characters, i.e. 16 more than the 12 of the variable name. Could it be put neater and more economical?

I think so. I would propose a shorter format: 
A simple prefix that simply just tells PHP that what immediately follows is a PHP variable whose value should be inserted into the HTML document, but that in all other regards this is just HTML code.
There should also be a closing suffix (but this is perhaps mostly to give an aesthetically balanced coding.)

E.g. a prefix like "<$php:" and a suffix like ">" (or whatever), so that one could just write something like

<$php:somevariable>

in the HTML code in order to have the value of $somevariable to be inserted there.

The 28 characters of the usual way has now become just 19 characters. And the whole snippet is much more concise and clearer: it gives a neater appearance within the surrounding HTML code, which facilitates for the developer/designer.

This would simply be so much more economic and neater than the usual way.

The idea can be extended:

For instance, in order to call a function somewhere in a piece of HTML code, one might perhaps do something like this:

<?php:somefunction($var1, $var2)>

Thomas

Reproduce code:
---------------
<?php

function goodbye($a) {
 echo "Goodbye " . $a . "!";
}

$greeting = "Hello World!";

?>

This is some HTML text.<br>
Followed by:<br>
<$php:greeting><br>
<$php:goodbye("world")><br>
End



Expected result:
----------------
This is some HTML text.
Followed by:
Hello World!
Goodbye world!
End


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-08 08:08 UTC] derick@php.net
Uhm, are you not aware that we have this already in the form of:

<?=     ?>

 [2009-07-08 10:19 UTC] tcjohans at riseup dot net
Thank you for your reply, Derick.

I was not aware of that.

However, it appears that the <?=   ?> tagging is available only if you have control of the php.ini file and can set the short_open_tag setting. 

Which creates a problem with portability of scripts and in any event does not make these tags available to all. I just tried it in my script and it did not work.
 [2009-07-08 10:29 UTC] tcjohans at riseup dot net
The PHP Manual says:

"Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags."

I think the request now becomes:

Short tags should be made available to all by enabling scripts to dynamically set the ini settings for short tags in their scripts.

Thomas

PS! Do you really have to call this "bogus"??
 [2009-07-08 10:47 UTC] tcjohans at riseup dot net
Actually, using <?php=variable> might be more efficient performance-wise than the current short tag format <?=variable?>. 

With <?php= the parser would just need to check once it opens for PHP parsing (after the <?php) if the next character is a = or not. If it is, it would go on to read and interpret a variable and then switch back to HTML after the closing tag, >.

I.e. the parser would not operate with TWO different PHP start tags - which I suppose slows down parsing -, but only ONE, <?php. The switch to "echo variable"-mode would be handled internally only the few times immediately after a <?php tag has been encountered.

Also, PHP would already from the outset know that it is just going to echo a variable value and then switch back to HTML parsing. (Is this the case with the current short tag format or can it continue to run other statements after the first, implicit echo (which, if so, would affect performance negatively)?).

Thomas
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 14:01:32 2024 UTC