PHP變數傳遞問題。

來源:互聯網
上載者:User
我include了一個a.PHP檔案,使用a.PHP的變數,但我寫了一個函數,在這個函數裡就不能訪問這個變數了,1、我用參數傳遞進去也沒有用;2、我在函數裡面include 了a.PHP,也是沒有用。
請問函數怎麼才可以訪問這個外部檔案a.PHP的變數?


回複討論(解決方案)

那要看你的代碼是怎麼寫的了

既然這麼想用 直接用全域變數 函數裡用global聲明下 好了。

無代碼無真相

代碼如下:

include_once("config.php");
echo $textTpl; //無顯示 $textTpl是config.php裡定義的一個變數
$resultS= a($textTpl); //無顯示

function a($textTpl)
{
echo $TextTpl;

}

?>

不知道你config.php是怎??,我?了一下,是可以拿到的,function a中 echo $TextTpl 改? $textTpl 就可以了。

config.php

 


include_once("config.php");echo $textTpl; //123$resultS= a($textTpl);  //無顯示function a($textTpl){  echo $textTpl; // ??改小?,你之前用大?了,?然不行}


?出 123123

變數是區分大小寫!

官話就是:變數是大小寫敏感的

應該是沒問題的。變數當然是區分大小寫。雖然php是弱類型的,不過還沒弱化到這個地步。

理論上應該是可以的。 不會到你這具體是怎麼出問題的

變數是區分大小寫!

官話就是:變數是大小寫敏感的


還需要再提示一下,具體在下面找
function a($textTpl) {  echo $TextTpl;}

這個是你寫錯了。

碰到這種情況,你可以把你include的檔案,直接整成源碼放在你頁面上頭,這樣查看就容易了

function a( $textTpl)
{
echo $TextTpl;

}

global聲明

那變數一定在 你另一個函數裡,還沒用global聲明

我寫了幾個函數,請幫我看下哪裡錯:
檔案名稱:function.php

function getJson_obj($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res,true);
}

function getAccess_token($APPID,$APPSECRET)
{ $json_obj = getJson_obj("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET);
return $json_obj['access_token'];
}

function getOauth2_obj($APPID,$APPSECRET,$CODE)
{ $json_obj = getJson_obj("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$APPID.'&secret='.$APPSECRET."&code=".$CODE."&grant_type=authorization_code");
return $json_obj;
}

function getOpenID($APPID,$APPSECRET,$CODE)
{ $json_obj =getOauth2_obj($APPID,$APPSECRET,$CODE);
return = $json_obj['openid'];
}

function getOauth_Access_Token($APPID,$APPSECRET,$CODE)
{ $json_obj =getOauth2_obj($APPID,$APPSECRET,$CODE);
return = $json_obj['access_token'];
}

?>

我在另一個檔案login.php裡 include_once("function.php");
然後這個login.php開啟就報500錯誤,去掉這個引用就正常,請問我些函數哪裡有問題?

return = $json_obj['openid'];
return = $json_obj['access_token'];
多了 =

你開啟錯誤顯示功能,自己就看到了
何必自己跟自己過不去?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.