Home  >  Article  >  Backend Development  >  PHP变量传递问题。

PHP变量传递问题。

WBOY
WBOYOriginal
2016-06-23 13:54:15785browse

我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

<?php$textTpl = '123';?>


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'];
多了 =

你打开错误显示功能,自己就看到了
何必自己跟自己过不去?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn