我是PHP,
我在PHP中有3个函数。
功能1:
function get_main_sector($segment_id)
{
$dbCon = new dbCon();
$connect = $dbCon->connect();
$result = mysql_query($qry);
while($row = mysql_fetch_object($result))
{
$sourcem[] = $row;
}
mysql_free_result($result);
$disconnect = $dbCon->disconnect();
if(isset($sourcem))
return $sourcem;
else
return ;
}
功能2:
function getSector($sector_id)
{
$dbCon = new dbCon();
$connect = $dbCon->connect();
$result = mysql_query($qry);
while($row = mysql_fetch_object($result))
{
$sourcem[] = $row;
}
mysql_free_result($result);
$disconnect = $dbCon->disconnect();
if(isset($sourcem))
return $sourcem;
else
return ;
}
功能3:
function getopinions($opinions)
{
$dbCon = new dbCon();
$connect = $dbCon->connect();
$result = mysql_query($qry);
while($row = mysql_fetch_object($result))
{
$sourcem[] = $row;
}
mysql_free_result($result);
$disconnect = $dbCon->disconnect();
if(isset($sourcem))
return $sourcem;
else
return ;
}
我的问题是我想在函数3中使用函数1和函数3的变量,因为我在函数3的查询中使用。但函数2和函数3的变量是本地的。 我可以在功能3中调用这些变量吗?如果是,那么请告诉我我该怎么办?
更新: 我将我的第一个函数称为:
if(isset( $_REQUEST['segmentID']))
{$segment_id = $_REQUEST['segmentID'];}
else {$segment_id=0;}
if(isset($segment_id) && $segment_id!=0)
{
$main_sectors = $cont->get_main_sector($segment_id);
echo '<select name="main_sector_id" id="main_sector_id" STYLE="width: 300px" onchange="get_sector(this.value);">';
echo '<option value="">[--Select Team----]</option>';
if(is_array($main_sectors ) && !empty($main_sectors ))
{
foreach($main_sectors as $main_sector)
{
echo '<option value="'.$main_sector->ID.'">';
echo $main_sector->title;
echo '</option>';
}
}
echo '</select>';
die;
}
答案 0 :(得分:2)
您必须在函数外部声明变量,然后在函数内部使用这些变量作为全局变量。看这里怎么做: