dongli1887 2012-09-25 14:00
浏览 244
已采纳

从php中的另一个页面调用一个函数

I am call a function which is defined in my smsconfig.php file like

$GLOBAL_URL_OF_THE_SITE= "http://webfaction";


function get_site_url(){
    return $GLOBAL_URL_OF_THE_SITE; 
}

In my index.php file, I am including that smsconfig.php file then I am doing the following:

  <?php 
    include("smsconfig.php");
    ?>
    <html>
    <head >
     <link rel="stylesheet" type="text/css" href="default.css" />
        <script src="<?php echo get_site_url();?>/UI/jquery/jquery-1.7.2.min.js" type="text/javascript"></script>

unfortunately my JS is not loading and I am getting the following error in firebug:

"NetworkError: 404 Not Found - http://webfaction/UI/%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20variable:%20GLOBAL_URL_OF_THE_SITE%20in%20%3Cb%3E/opt/lampp/htdocs/UI/smsconfig.php%3C/b%3E%20on%20line%20%3Cb%3E11%3C/b%3E%3Cbr%20/%3E/UI/jquery/jquery-1.7.2.min.js"

Please tell me what I am doing wrong ?

  • 写回答

3条回答 默认 最新

  • dongyanling9248 2012-09-25 14:02
    关注

    Functions do not have access to global variables by default. You have to declare the variable with global before accessing it:

    $GLOBAL_URL_OF_THE_SITE= "http://webfaction";
    
    function get_site_url(){
        global $GLOBAL_URL_OF_THE_SITE;
    
        return $GLOBAL_URL_OF_THE_SITE; 
    }
    

    Without global, you should be seeing a warning, which you may be suppressing:

    PHP Notice: Undefined variable: GLOBAL_URL_OF_THE_SITE

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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用