Use PHP `variables value` quickly in HTML
I often use HTML templa开发者_如何学编程te for my application and within the template content there are place holder
which I marked as <?php echo $myVar; ?>
Is there a shorter syntax for that? I tried <?php=$myVar?>
but didn't work :D
Please give a hint if you know some way. Thank you!
<body>
<?php $myVar = 122; ?>
abb
<div>
<?php echo $myVar; ?>
</div>
<div>
<?php=$myVar?>
</div>
ccc
</body>
<?=$myvar ?>
short_open_tag should be On.
You can use short tags if enabled.
<?= $var; ?>
This is the same as doing this:
<?php echo $var; ?>
To set this up you can find info here, however it's not recommended you use this method.
http://php.net/manual/en/ini.core.php
If ASP tags are enabled you can also do things like this:
<%=$var; %>
<% echo $var; $>
The shorter version is <?=$myVar;?>
, but please DON'T do this! :(
Quoting from the comment, in case anyone misses it.
because:
This will not allow much flexibility in moving the servers, i.e. you must control the server or be allowed to change the ini directives [to turn on short_open_tag], otherwise you are doomed.
It might be deprecated in the future.
Readability is not a trade for functionality. Period.
it is
<?=$myVar; ?>
short tag should be enabled with PHP
If you are going to use inline xml with PHP do not enable short tags.
As documented here
If you want to use PHP in combination with XML, you can disable this option in order to use inline. Otherwise, you can print it with PHP, for example: '; ?>. Also, if disabled, you must use the long form of the PHP open tag ().
You could use double quote echo like this:
<?php
$myVar = 122;
echo "
<body>
abb
<div>
whatever and $myVar displayed here
</div>
<div>
The content is: $myOtherVar
</div>
sometext
</body>
";
?>
But consider using your template with OOP. It would be even cleaner:
Template::header();//Echoes your template header
Template::content($myContentText,$myWhateverVar);//Echoes your template filled with your vars
HTH, caffein
精彩评论