-2

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
Monty
  • 1,110
  • 7
  • 15

3 Answers3

1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Pipe output from CLI to browser, that's normally what a webserver is for. Also I wrote you will moan about that. And no, you did *not* find a different way. You imagined something like "what if it would be that there would be PHP code without PHP tags" which is somewhat like uh, this is not how PHP works as the PHP tags are an elementary part of the languages syntax to keep static text (HTML) and PHP code apart. You can leave out the closing tag by the way. – hakre Dec 24 '15 at 09:13
1

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.

SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • Would you say that the example you have there does require again the PHP tags? :) – hakre Dec 24 '15 at 09:14
  • @hakre in the end, yes, at least one file with tags... unless patching PHP itself or modifying the PHP files before they reach mod_php / fpm. – SirDarius Dec 24 '15 at 09:17
  • There might be a rotting patch from the [abondonned RFC "Source Files Without Opening Tag"](https://wiki.php.net/rfc/source_files_without_opening_tag) already ;) – hakre Dec 24 '15 at 09:21
  • Or OP could use a text editor or IDE that hides the tags. Most IDEs already implement code folding... – SirDarius Dec 24 '15 at 09:24
  • Now that would leave him without "finding the different way", so this is perhaps not an option in OPs eyes but I have to admit, your suggestion sounds enormous practical. Perhaps on a light day, it will be given a try. – hakre Dec 24 '15 at 09:27
0

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.

Matt
  • 2,851
  • 1
  • 13
  • 27
  • Well, that's unfortunate. – Matt Dec 24 '15 at 08:51
  • @Monty You are, a bit, wrong. Frameworks and CMS use _core php_ too - and only because they depend on only _core php_. Object-oriented programming is part of _core php_ too. Only extensions (that are not part of default installations, and that may be platform dependent) are not _core PHP_. So, your rejecting frameworks and CMS with saying about using of _core php_ is a bit nonsense. – Václav Dec 24 '15 at 09:11
  • @Václav : I know framework and cms based on CORE PHP. but i does not want to use framework or cms for this little query. – Monty Dec 24 '15 at 09:14
  • @Monty I don't see anything wrong on using of ?> or . And instead frameworks and cms (because my knowledge of some _programming styles_ is not so great) I use my own generator of html code. – Václav Dec 24 '15 at 09:25
  • @Václav there is no wrong to write code in php start/end tags. Leave it. its not a major query it was just small kind of looking different way, something new in php. – Monty Dec 24 '15 at 09:32