-1

PHP include Variable Fail. So i have problem with php variable inclusion, in php files where i need to call for specific variable in another php file, i dont always get the requested variable. And i dont understand why.

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>";

?>

I get Notice: "Undefined variable: symbol in ..\database.php on line 7" "MUSIC-----------------"

In one place it takes tha variable but in other it doesnt. Why? How to fix that?

  • Some sensible indention and formatting would make this code much easier to read, and in turn, easier to troubleshoot. – Qirel Aug 27 '17 at 12:04
  • $nonce=time(); Its is declared in function. – Eduards Silins Aug 27 '17 at 12:05
  • $uri="https://something.com?key='$key'&symbol='$symbol'&nonce='$nonce'"; – Zast Aug 27 '17 at 12:06
  • how are you using the GET arrays? Check if all are set/not empty. – Funk Forty Niner Aug 27 '17 at 12:06
  • the selected function is being called from inside the balancedb.php file, is the settings.php file being included inn to this file? – Zast Aug 27 '17 at 12:10
  • you also seem to be using this with a database, so if you're not showing relevant code for this, we can't help you. How is `info.php` include/used also? and what is `balancedb.php`? – Funk Forty Niner Aug 27 '17 at 12:10
  • `function selected($key, $secret, $symbol){` and `$info = selected($key, $secret, $symbol);` – Federkun Aug 27 '17 at 12:11
  • @Federkun funny you should post that ^, I deleted the first comment I posted about using 2 arguments in the method, rather than 3. I shouldn't have deleted it. – Funk Forty Niner Aug 27 '17 at 12:12
  • function selected($key, $secret) re-declares the $key, $secret variable, which are being passed to it from elsewhere. including the secrets file above the function definition would be irrelevant! – Zast Aug 27 '17 at 12:14
  • @user3633383 Right, to which I posted a comment about it most likely related to database work, which we don't know how it's used. The question is unclear for me. I think they're missing an argument in the method. – Funk Forty Niner Aug 27 '17 at 12:16

2 Answers2

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>";

 ?>
Zast
  • 492
  • 2
  • 7
  • 22
0

That's because of the Variable scope. You don't have access to settings.php's $symbol (global scope) inside selected.

What to do? Pass it as an argument:

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

Then when you call it

// pass settings.php's $symbol here
$info = selected($key, $secret, $symbol);

For more information, read the documentation about how the scope of a variable works.

Federkun
  • 36,084
  • 8
  • 78
  • 90