PHP包括Variable Fail

时间:2017-08-27 12:00:09

标签: php variables include

PHP包括变量失败。 所以我有php变量包含的问题,在php文件中我需要调用另一个php文件中的特定变量,我总是得到所请求的变量。我不明白为什么。

的settings.php

<?php
$symbol = 'MUSIC';
$key='xxx';
$secret='xxx';
?>

database.php中

<?php
include ('settings.php');


function selected($key, $secret){
$nonce=time();
$uri='https://something.com?key='.$key.'&symbol='.$symbol.'&nonce='.$nonce; //Problem is in here


$sign=hash_hmac('sha512',$uri,$secret);
$ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$execResult = curl_exec($ch);
$obj = json_decode($execResult, true);
$info = $obj["result"]["info"];
return $info;

info.php的

<?php include "balancedb.php"; 

$url = "https://somthingother.com/";
$fgc = json_decode(file_get_contents($url), true);

$info = selected($key, $secret);
    echo "<br>".$symbol."-----------------".$info."<br>";

?>

我收到通知:“未定义的变量:第7行的.. \ database.php中的符号” “MUSIC -----------------”

在一个地方,它需要变量,但在其他地方它不会。为什么?如何解决?

2 个答案:

答案 0 :(得分:0)

 <?php 
 include_once( "settings.php" ); //this must be included here, before calling the selected($key, $secret); function below!!!

 include_once( "balancedb.php" ); 

 $url = "https://somthingother.com/";
 $fgc = json_decode(file_get_contents($url), true);

 $info = selected($key, $secret);
 echo "<br>".$symbol."-----------------".$info."<br>";

 ?>

答案 1 :(得分:0)

那是因为Variable scope。您无法访问settings.php$symbol的{​​{1}}(全局范围)。

怎么办?将其作为论据传递:

selected

然后当你打电话时

function selected($key, $secret, $symbol) {

有关更多信息,请阅读有关变量范围如何工作的文档。