doujia4619 2012-11-11 15:47 采纳率: 0%
浏览 33
已采纳

如何将数组作为参数传递给函数[关闭]

I made this code where I define an array, fliepath, in which I store the locations of some files.

     include 'last_file.php'; // Include the function last_file
     $last_file = last_file();  // assign to the function a variable and call the  function last_file
    // Connect to the database
     include('connect_thesis.php');

    // Defining an array, which has the three paths to the three different gps receivers
    $file_path[0] = "/Applications/MAMP/htdocs/php_test/check/".$last_file[0];
    //echo $file_path[0]; echo "<br>";
    $file_path[1] = "/Applications/MAMP/htdocs/php_test/check2/".$last_file[1];
    //echo $file_path[1]; echo "<br>";
    $file_path[2] = "/Applications/MAMP/htdocs/php_test/check3/".$last_file[2];
    //echo $file_path[2]; echo "<br>";

Then I made a function called insert() which I want to take as input the $file_path[0]:

     function insert($file_path){

     $fh = fopen($file_path,'r') or die ("Could not open:".mysql_error()).......;

I call the function from the main script as:

         insert($file_path[0]);

I am new in programming and I am sure somewhere I am missing something basic! The problem is that the function doesn't run!!! Can you help me? Thanx D.

I THINK I DONT PASS CORRECTLY THE VALUE TO THE FUNCTION. CAUSE I GET NOTHING AS AN ERROR!

  • 写回答

2条回答 默认 最新

  • dongyaobo9081 2012-11-11 16:13
    关注

    A few points to note:

    You are calling insert using only the index 0, consider using a foreach and call the function on each items in your array.

    insert() -> we are missing part of the implementation, but if the file exists, you should not get an error. Keep in mind that you need to close files that you open.

    or die -> it looks like you copy pasted code from elsewhere... mysql_error() will not help you much as you're dealing with files at the moment. Consider changing it to

    $fh = fopen($file_path,'r') or die ("Could not open:".$file_path)
    

    You should probably handle graciously the error instead of using "die"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测