1

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.

Halis
  • 35
  • 7

4 Answers4

2

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
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • I just created my account and I need 15 reputation to upvote, but I will do it whenever I get 15. – Halis Jun 11 '14 at 14:22
0

Your notice will only be shown in some cases, specifically when you have an odd number of elements in your csv file.

Take a look at these two lines:

while ($i < count($comment_array)){
    // ...
    $commentator_comment = $comment_array[$i+1];
    // ...

The while condition will guarantee that the index $i will exist, but it makes no such guarantee for the index $i+1. If you have only 1 item in your csv file, it will display the notice. If you have 2 items, you won't see the notice.

The solution by @Giacomo1968 will work, but to offer an alternative solution (that will operate on all pairs of inputs, as you're intending), then you can simply change your while condition to this:

while( ($i + 1) < count($comment_array) ) {
    // ...

This has the side effect that, should you have an odd number of entries (basically a comment name entry with no associated comment), then it will ignore completely that last comment, which may be more beneficial to you than processing a comment name with no comment.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
0

Short syntax:

$commentator_name = isset($comment_array[$i]) ? $comment_array[$i] : '';

$commentator_comment = isset($comment_array[$i+1]) ? $comment_array[$i+1] : '';

On your local development environment, set error_reporting = E_ALL. Since you are getting the notice its already set.

Vladd
  • 124
  • 5
0

I think is because you is trying access the offset that do not exists on last interaction of the loop. Try this:

<?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)-1)){

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

        echo "$commentator_name" . "$commentator_comment";


        $i = $i + 2;

    }
?>

Alternativaly, you can try too:

while ($i < count($comment_array){

        $commentator_name = $comment_array[$i];
        if ($i < (count($comment_array)-1)) {
            $commentator_comment = $comment_array[$i+1];
        }

        echo "$commentator_name" . "$commentator_comment";


        $i = $i + 2;

    }
RibeiroSt
  • 681
  • 5
  • 6