我需要在另一个文件example.php中调用index.php中的函数。如果我使用include,它也会从index.php获取所有的html。我只想要函数的结果。有办法做到这一点吗?
$rs = odbc_exec($con, $sql);
if (!$rs) {
exit("There is an error in the SQL!");
}
$data[0] = array('D','CPU_Purchased_ghz');
$i = 1;
while($row = odbc_fetch_array($rs)) {
$data[$i] = array(
$row['D'],
$row['CPU_Purchased_ghz']
);
$i++;
}
//odbc_close($con); // Closes the connection
$json = json_encode($data); // Generates the JSON, saves it in a variable
echo $json;
基本上,index.php中的这段代码从查询数据库的文件中获取信息,并将其编码到json中。我不想回显,而是想做一个回显json的函数,并在一个新文件中调用它,以便只在页面上显示json。
发布于 2015-03-27 17:05:41
创建一个functions.php
文件。将函数添加到该文件,并将该文件包含在example.php
文件中
发布于 2015-03-27 17:08:13
index.php
function myFunction() {
return "It works!";
}
example.php
include('index.php');
echo myFunction();
发布于 2015-03-27 17:14:43
在调用函数之前包含该文件。
参见以下示例:
index.php
<?php
function myFunction() { //function .
return "FirstProgram"; //returns
}
?>
现在使用include http://php.net/include来包含index.php,以使其内容可用于第二个文件:
example.php
<?php
include('index.php');
echo myFunction(); //returns myFunction();
?>
https://stackoverflow.com/questions/29296775
复制相似问题