繁体   English   中英

运行包含的PHP文件

[英]Running an included PHP file

我有一个PHP脚本(我将调用主脚本),其中包含另一个脚本。 伪代码是这样的

include '.../somescript.php'

if(some conditions are met){
          show the contents of somescript.php
}

我只希望这个脚本显示在屏幕上(不会重定向到该页面)。 它主要是HTML,但我不确定显示内容的最佳方式。 我可以将它包装在一个函数showScript()中,它回显所有HTML,并在我的主脚本中调用此函数:

include '.../somescript.php'

if(some conditions are met){
          showScript();
}

但是我想知道rit是否可以直接运行脚本作为我的主脚本的补充? 如果最后一个短语听起来有点乱,我很抱歉,我不确定如何正确表达我的意思。

如果“somescript.php”文件都是HTML,您只需执行以下操作:

<?php
  if (some condition) {
      include('../somescript.php');
  }
?>

这将使“somescript.php”的内容显示在屏幕上,如果它都是纯HTML。 如果该文件中有PHP,它将在满足“某些条件”并且包含该文件时执行(所有函数声明等都可以在该点之后访问)。

如果你问别的话,请告诉我,我误解了你的问题......

创建两个单独的脚本,其中包括所有sql查询和可以在HTML上显示的输出数组..另一个文件只作为HTML文件,然后在mainscript.php之上包含query.php,在if语句下包含showhtmlscript。 php包含html,

来自query.php的数组变量输出,比如$ output,可以在showhtmlscript.php中使用

<table>
<tr><td>$output['name']</td><td>$output['age']</td></tr>
</table>

你需要这个

include(query.php);

if(something is set){
include(somehtmlscript.php);
}

如果我对你不好,你可以使用缓冲。

ob_start();
include '.../somescript.php'

if(some conditions are met){
  echo ob_get_contents();
}
ob_end_clean();
<?php
  if (some conditions are met) {
      include('../somescript.php');
  }
?>

或选择输出缓冲

<?php
ob_start();
include '.../somescript.php'

if(some conditions are met){
  echo ob_get_contents();
}
ob_end_clean();
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM