write php code like :
$w = "World";
echo "Hello $w";
But I don't want to write this PHP code having PHP start and end tag
<?php
$w = "World"; echo "Hello $w";
?>
OR
<?
$w = "World"; echo "Hello $w";
?>
OUTPUT in browser :
Hello World
write php code like :
$w = "World";
echo "Hello $w";
But I don't want to write this PHP code having PHP start and end tag
<?php
$w = "World"; echo "Hello $w";
?>
OR
<?
$w = "World"; echo "Hello $w";
?>
OUTPUT in browser :
Hello World
Only you want something won't make PHP provide it for you (even I know from some of the core developers personally (but deniable) that they strive hard to make Monty happy - but it's perhaps someone else like you ;) ).
There are at least two existing RFCs (of which one has been abandoned) that already put what you outline in your question on the discussion table:
Apart from these RFCs - but I'm sure you'll moan about it - PHP actually provides what you're looking for, it's just that you will dislike it:
PHP will execute the code without the PHP tags when you use the CLI binary with the appropriate command-line arguments:
-r <code> Run PHP <code> without using script tags <?..?>
You only need to send the output to the browser.
Apart from that, you can open your file without tags, add the PHP tag in front (you don't need the closing one) and then execute that code. As this is an operation close to eval()
(or include()
) for that matter, I would normally not suggest that. But I'm quite sure you should be able to find questions regarding that on SO already with some examples in the answer. Just take caution, you will most likely shoot in your own foot if you're not entirely clear about what you're doing. Just saying, learned it the hard way.
Only a dirty hack can make this possible.
So, ok, let me give you a dirty hack:
function include_untagged($filename) {
$code = file_get_contents($filename);
return eval($code);
}
The eval()
function does the trick, because, according to the doc:
The code must not be wrapped in opening and closing PHP tags, i.e.
'echo "Hi!";'
must be passed instead of<?php echo "Hi!"; ?\>
.
So you can just use a front-facing index.php using tags, and write the rest of your files without those by using this function.
Then again, I would never do this. I agree that PHP isn't used in the same way as when it was originally designed, and code tags don't make much sense when the PHP file is not a template. However, this is swimming against the current. There is no real benefit in that approach apart from saving a few bytes, and other PHP developers looking at the code will be utterly confused.
Use a PHP framework like Laravel and you can easily echo out variables into views like {{$variable}}
. You cant really just not use php tags as the processor won't know what is HTML and what is PHP.