包括PHP的动态HTML [英] Including dynamic HTML with PHP
问题描述
我将PHP变量放在包含的HTML文件中。执行此操作的最佳方法是什么?
I have PHP variables to place within an included HTML file. What's the best way of executing this?
//contents of file1.php
$variable1 = "text 1";
$variable2 = "text 2"
$newContent = include('file2.php');
echo $newContent;
//contents of file2.php
<p>standard HTML with PHP... <strong><?=$variable1?></strong></p>
<p><?=$variable2?></p>
这种通用方法被认为是正常的,但这里的实际代码不起作用。我是否使用file_get_contents()或include(),如何在包含文件中执行PHP以输出正确的内容?
This general method is considered OK but the actual code here doesn't work. Do I use file_get_contents() or include(), how do I execute the PHP within the includes file to output the correct contents?
我是否应该使用 HTML>>>
推荐答案
你做得很好,你会发现大多数人都使用相同的方法。我个人不会使用PHP短标签(有些主机不启用它),但这是一个偏好的问题。
What you're doing is fine, and you'll find that most people use the same exact method. I personally wouldn't use PHP short tags (some hosts don't enable it), but that's a matter of preference.
编辑:根据您的编辑,您似乎没有启用短标签。检查你的ini( http://php.net/manual/en/ini.core。 PHP )。但是你确实不应该使用短标签,因为正如clownbaby所提到的,PHP 6将弃用它们。即使您不关心将来验证代码,它们仍然很麻烦(这很明显,因为您的代码不起作用)。切换到<?php echo $ variable1; ?>
你会没事的。
As per your edit, it seems like you don't have short tags enabled. Check your ini (http://php.net/manual/en/ini.core.php). But you really shouldn't be using short tags, because as clownbaby mentions, PHP 6 will deprecate them. Even if you don't care about future proofing your code, they're still troublesome (which is evident because your code isn't working). Switch to <?php echo $variable1; ?>
and you'll be fine.
这篇关于包括PHP的动态HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!