phpnotice

Notice Undefined offset


Im new to php and having some trouble with a notice. "Notice: Undefined offset: 1" The problem is on line 38:

  $commentator_comment = $comment_array[$i+1];

When I wrote this code I worked on an old laptop where I had to work on a server, since WAMP etc. didnt work, and I didnt get the notice then. Its awhile ago I wrote it, but Im trying my best to find the problem without luck.

<?php
$filename = "data/comments.txt";
    if( isset( $_POST["name"]) && isset( $_POST["comment"])) {
$comment_name = @$_POST["name"];
$comment_comment = @$_POST["comment"];
$theComment = "<div id='comment'><p><b>" . $comment_name . "</b><br>" . $comment_comment . "</p></div>";

file_put_contents($filename, $theComment, FILE_APPEND);}
?>

<?php
$comments_from_file = file_get_contents($filename);
$comment_array = str_getcsv($comments_from_file, "|");
$i = 0;
while ($i < count($comment_array)){
    $commentator_name = $comment_array[$i];
    $commentator_comment = $comment_array[$i+1];

    echo "$commentator_name" . "$commentator_comment";


    $i = $i + 2;

}?>

Thanks in advance on all help, its appriciated.


Solution

  • Easy fix for the “Notice”: Just check if that index exists. So this piece of code:

    $commentator_name = $comment_array[$i];
    $commentator_comment = $comment_array[$i+1];
    

    Changes to this:

    $commentator_name = $commentator_comment = '';
    if (isset($comment_array[$i])) {
      $commentator_name = $comment_array[$i];
    }
    if (isset($comment_array[$i+1])) {
      $commentator_comment = $comment_array[$i+1];
    }
    

    When I wrote this code I worked on an old laptop where I had to work on a server, since WAMP etc. didnt work, and I didnt get the notice then.

    It could be that the error reporting level between the new server setup and whatever the old server setup. Meaning the new server is set to report PHP notices, but the old one didn’t.

    To adjust the settings for error reporting you can start by going into your php.ini and looking for the error_reporting line. On Ubuntu 12.04 & other Linux installs, the php.ini is located here:

    /etc/php5/apache2/php.ini
    

    Typically that line is just set to E_ALL like this:

     error_reporting = E_ALL
    

    To disable notices you can adjust that line to this:

     error_reporting = E_ALL & ~E_NOTICE
    

    And you can even add more options to disable by chaining them to the end of that line like this:

     error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING
    

    The full list of PHP error level constants should be in that php.ini file. Here is a copy if the ones that are shown in the comments of a typical php.ini file:

    ; Error Level Constants:
    ; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 6.0.0)
    ; E_ERROR           - fatal run-time errors
    ; E_RECOVERABLE_ERROR  - almost fatal run-time errors
    ; E_WARNING         - run-time warnings (non-fatal errors)
    ; E_PARSE           - compile-time parse errors
    ; E_NOTICE          - run-time notices (these are warnings which often result
    ;                     from a bug in your code, but it's possible that it was
    ;                     intentional (e.g., using an uninitialized variable and
    ;                     relying on the fact it's automatically initialized to an
    ;                     empty string)
    ; E_STRICT          - run-time notices, enable to have PHP suggest changes
    ;                     to your code which will ensure the best interoperability
    ;                     and forward compatibility of your code
    ; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
    ; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
    ;                     initial startup
    ; E_COMPILE_ERROR   - fatal compile-time errors
    ; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
    ; E_USER_ERROR      - user-generated error message
    ; E_USER_WARNING    - user-generated warning message
    ; E_USER_NOTICE     - user-generated notice message
    ; E_DEPRECATED      - warn about code that will not work in future versions
    ;                     of PHP
    ; E_USER_DEPRECATED - user-generated deprecation warnings