dsy19890123 2012-03-02 07:14
浏览 53
已采纳

PHP:包含没有输出缓冲的字符串?

Say there's an if statement:

if (stripos($names, "jack") !== false || stripos($names, "bob") !== false) {
 echo "Jack or Bob found.";
}

I'd like to use this statement across multiple pages without having to edit the if statement in each page if I wanted to change the if parameters.

I've tried this:

$contents = file_get_contents('names.php');
if ($contents) {
 echo "Jack or Bob found.";
}

names.php
<?php
 $return_me = stripos($names, "jack") !== false || stripos($names, "bob") !== false;
 return $return_me;
?>

And it doesn't work. I'm trying to get this done without using output buffering because it screws up my entire script. Does anyone know a solution?

  • 写回答

1条回答 默认 最新

  • dongtao9158 2012-03-02 07:21
    关注

    I suggest you write a function for this in a utility file you can include.

    utils.php.inc

    function check_found_user($names) {
        return (stripos($names, "jack") !== false || stripos($names, "bob") !== false);
    }
    

    Your other source code files

    require_once('utils.php.inc');
    
    if ( check_found_user($names) ) {
        echo "Jack or Bob found.";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵