命令行编程时出错

时间:2012-01-08 16:07:40

标签: php command-line

当我尝试使用命令行处理文件时:

cat texte.txt|php index.php

我得到了这个奇怪的堆栈:

C:\wamp\www\Fraud Prevention>cat texte.txt|php index.php
PHP Notice:  Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li
ne 92
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li
ne 94
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li
ne 96
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li
ne 98
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li
ne 100
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10
0

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li
ne 102
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10
2

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li
ne 104
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10
4

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li
ne 92
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li
ne 94
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li
ne 96
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li
ne 98
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li
ne 100
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10
0

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li
ne 102
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10
2

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li
ne 104
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10
4

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li
ne 92
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li
ne 94
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li
ne 96
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li
ne 98
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98


Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li
ne 100
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10
0

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li
ne 102
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10
2

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

PHP Notice:  Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li
ne 104
PHP Stack trace:
PHP   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10
4

Call Stack:
    0.0005     330608   1. {main}() C:\wamp\www\Fraud Prevention\index.php:0

修改

这是我在92和104行之间的代码:

    $handle = fopen ("php://stdin","r");

    $N = $title = fgets($handle, 1024);


    while(!feof($handle))
    {
      if (!$order = fgets($handle)) continue;
      $all_orders[] = explode(',', $order);

    }

    foreach ($all_orders as &$order){

    $order["Order id"]=$order[0];
    unset($order[0]);
    $order["Deal id"]=$order[1];
    unset($order[1]);
    $order["Email address"]=$order[2];
    unset($order[2]);
    $order["Street address"]=$order[3];
    unset($order[3]);
    $order["City"]=$order[4];
    unset($order[4]);
    $order["State"]=$order[5];
    unset($order[5]);
    $order["Zip Code"]=$order[6];
    unset($order[6]);
    $order["Credit Card"]=$order[7];
    unset($order[7]);

    }

print_r($all_orders);

1 个答案:

答案 0 :(得分:3)

在foreach循环中,您正在访问数组值,而不检查它们是否存在。

每当你访问一个不存在的索引时,PHP都会给你一个通知 - 在你的情况下,是一个堆栈跟踪(我假设它是XDebug)。通知不是一个showstopper错误,你的脚本只是继续,并获得另一个通知,而另一个,等等。

您可以使用isset($order[$index])检查索引是否存在。

此外,您似乎正在阅读CSV数据。使用php的内置fgetcsv()如何处理你的代码所没有的边缘情况呢?

顺便说一下,你的foreach循环是不必要的,你可以在读取记录后在while循环中进行处理。