我有以下的代碼和他們沒有工作:PHP遞歸函數工作不正常:(
的index.php:
include("loadData.php");
$my_var = loadData("myTxt.txt");
var_dump($my_var);
loadData.php:
function loadData($my_file){
if(file_exists($my_file)){
$file_contents = file_get_contents($my_file);
$file_contents = json_decode($file_contents);
}else{
// If file doesn't exist, creates the file and runs the function again
$data_to_insert_into_file = simplexml_load_file("http://site_with_content.com");
$fp = fopen($my_file, "w");
fwrite($fp, json_encode($data_to_insert_into_file));
fclose($fp);
// Since the file is created I will call the function again
loadData($my_file);
return;
}
// Do things with the decoded file contents (this is suposed to run after the file is loaded)
$result = array();
$result = $file_contents['something'];
return $result;
}
這第二次(在創建文件後)按預期工作,我可以在index.php上顯示信息,但是在第一次運行時(在創建文件之前)它始終顯示$ result爲NULL,我不能明白爲什麼我打電話給th e功能再次...
任何想法?
謝謝
修復你的返回語句返回正確的值... – Phantom
謝謝你的幫助:) – user2894688