Php 无法识别函数中的变量
我试图在url中回显时在函数中传递一个变量以进行解析,但是我收到错误,说它没有定义。变量$plex_令牌Php 无法识别函数中的变量,php,function,variables,Php,Function,Variables,我试图在url中回显时在函数中传递一个变量以进行解析,但是我收到错误,说它没有定义。变量$plex_令牌 function getPlexXML($url) { libxml_use_internal_errors(true); $plex_token = 'xxxxxxxxxx'; $xml = simplexml_load_file($url); $count = number_format((float)$xml['totalSize']); if (false === $xml) {
function getPlexXML($url)
{
libxml_use_internal_errors(true);
$plex_token = 'xxxxxxxxxx';
$xml = simplexml_load_file($url);
$count = number_format((float)$xml['totalSize']);
if (false === $xml) {
echo '<div class="counter_offline">N/A</div>';
} else {
echo '<div class="counter_live">'.$count.'</div>';
}
}
echo getPlexXML('https://plex.example.com/library/sections/5/all?type=1&X-Plex-Container-Start=0&X-Plex-Container-Size=0&X-Plex-Token='.$plex_token.'');
函数getPlexXML($url)
{
libxml\u使用\u内部错误(true);
$plex_令牌='XXXXXXXXX';
$xml=simplexml\u加载文件($url);
$count=number_格式((float)$xml['totalSize']);
if(false==$xml){
回音‘不适用’;
}否则{
回显“.$count.”;
}
}
echo getPlexXML('https://plex.example.com/library/sections/5/all?type=1&X-丛容器开始=0&X-Plex-Container-Size=0&X-Plex-Token='.$Plex_Token'.');
这是因为您引用的$plex\u令牌不在定义它的上下文中 您在函数“getPlexXML”内部定义了它,但在该函数外部使用它作为传递到getPlexXML的参数 您可以执行以下操作之一: A) 在函数之外定义它,因为您没有在函数中使用它:
$plex_token = 'xxxxxxxxx';
function getPlexXML($url){
// You cannot use $plex_token in here
...
}
echo getPlexXML('https://plex.example.com/library/sections/5/all?type=1&X-Plex-Container-Start=0&X-Plex-Container-Size=0&X-Plex-Token='.$plex_token.'');
$plex_token = 'xxxxxxxxx';
function getPlexXML($url){
global $plex_goken;
// You can use $plex_token in here
...
}
echo getPlexXML('https://plex.example.com/library/sections/5/all?type=1&X-Plex-Container-Start=0&X-Plex-Container-Size=0&X-Plex-Token='.$plex_token.'');
或B)将其设为全局变量,然后可以在函数内部或外部使用:
$plex_token = 'xxxxxxxxx';
function getPlexXML($url){
// You cannot use $plex_token in here
...
}
echo getPlexXML('https://plex.example.com/library/sections/5/all?type=1&X-Plex-Container-Start=0&X-Plex-Container-Size=0&X-Plex-Token='.$plex_token.'');
$plex_token = 'xxxxxxxxx';
function getPlexXML($url){
global $plex_goken;
// You can use $plex_token in here
...
}
echo getPlexXML('https://plex.example.com/library/sections/5/all?type=1&X-Plex-Container-Start=0&X-Plex-Container-Size=0&X-Plex-Token='.$plex_token.'');