drdu53813 2013-10-09 17:24
浏览 152

通过另一个文件调用PHP函数

I created a simple sample php file for display some function outputs. here is the code..

<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');

switch (key($_GET)){
    case 'red':
        $printHeader = pageHeader('red','on');
        $printName = hello('Joel');
        break;

    case 'blue':
        $printHeader = pageHeader('blue','off');
        $printName = hello('Duck');
        break;

    case 'yellow':
        //$printHeader = pageHeader('yellow','on');
        break;
}


function hello($name){
    return $name;
}


function pageHeader($header,$onoff){
    if ($onoff == 'on') {
        return $header."page header<br>";
    }
    else {return null;}
}

echo $printHeader;
echo $printName;

?>

This code is working fine without any problems.

When I call 'example.com/php/tipo34.php?red', it shows on the screen:

redpage header
Joel

And when I call 'example.com/php/tipo34.php?blue' it shows on the screen:

Duck

I tried to put the below functions inside of another php file called tipo34-req.php and received the following error:

Fatal error: Call to undefined function pageHeader() in C:\wamp\www\php\tipo34.php on line 8

The code I tried:

<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
    case 'red':
        $printHeader = pageHeader('red','on');
        $printName = hello('samitha');
        break;

    case 'blue':
        $printHeader = pageHeader('blue','off');
        $printName = hello('kalum');
        break;

    case 'yellow':
        //$printHeader = pageHeader('yellow','on');
        break;
}

include 'tipo34-req.php';

echo $printHeader;
echo $printName;

?>

tipo34-req.php code:

<?php
function hello($name){
    global $name;
    return $name;
}

function pageHeader($header,$onoff){ global $header, $onoff
    if ($onoff == 'on') {
        return $header."page header<br>";
    }
    else {return null;}
}
?>

How do I solve this problem? Using the function directly on the file works, but when I put the functions in another php file, it throws the error.

Thanks.

  • 写回答

2条回答 默认 最新

  • doudou348131346 2013-10-09 17:28
    关注

    Include your file above its contents usage. PHP is unaware of the functions since they are included later in the code.

    include 'tipo34-req.php';
    
    switch (key($_GET)){
        case 'red':
            $printHeader = pageHeader('red','on');
            $printName = hello('samitha');
        break;
    
        case 'blue':
            $printHeader = pageHeader('blue','off');
            $printName = hello('kalum');
        break;
    
        case 'yellow':
            //$printHeader = pageHeader('yellow','on');
        break;
    
    
    }
    
    echo $printHeader;
    echo $printName;
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条