dounuo8797 2015-04-20 14:35
浏览 23
已采纳

尝试使用另一个php文件中的函数时出现PHP致命错误

I have looked through some of the other questions on here about including variables from another php file but I've had no luck.

I am trying to include the return of a function in my <title></title>

randomtitle.php

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

Here is my index.php

index.php

<!DOCTYPE html>
<html>
<head>
    <?php require_once('randomtitle.php'); ?>
    <title><?php echo generateRandomString(); ?></title>
</head>
<body>
</body>
</html>

I get a fatal error Call to undefined function generateRandomString(). Also the actual randomtitle.php file gets displayed in the body of my index.php.

  • 写回答

3条回答 默认 最新

  • dowbwrr3590709 2015-04-20 14:39
    关注

    Your randomtitle file is missing the <?php ... ?> tags. Without that, PHP will NEVER execute the code within, and treat everything as plain output.

    Remember that there is no such thing as a "php script". There are only files with <?php ... ?> code blocks within.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示