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条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路