eva(...)l errors what's the best way to output?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sims

    eva(...)l errors what's the best way to output?

    Hi,

    I have an eval(...) and sometimes, (not often), i get an error.

    I can catch the error quite easily,
    if( FALSE === eval($code ))
    {
    // output the code
    echo ( htmlentities($c ode );
    }

    That does give me the error and the code but how can i get the code to be
    nicely lined up, or even have line numbers in front?
    Just to make it easier to debug.

    Many thanks

    Sims.



  • Pedro Graca

    #2
    Re: eva(...)l errors what's the best way to output?

    ["Followup-To:" header set to comp.lang.php.]
    Sims wrote:[color=blue]
    > I have an eval(...) and sometimes, (not often), i get an error.
    >
    > I can catch the error quite easily,
    > if( FALSE === eval($code ))
    > {
    > // output the code
    > echo ( htmlentities($c ode );
    > }
    >
    > That does give me the error and the code but how can i get the code to be
    > nicely lined up, or even have line numbers in front?
    > Just to make it easier to debug.[/color]

    Maybe like this? :)

    <?php
    $code = 'echo 7*3;
    echo 7#3; // error! the # makes this the same as "echo 7 echo 7+3"
    echo 7+3; // this is the line where the error is reported
    return true;';

    if (FALSE === eval($code)) {
    echo '<pre>';
    $code2 = explode("\n", htmlentities($c ode));
    for ($i = 1; $i <= count($code2); ++$i) {
    echo "$i\t{$code 2[$i-1]}\n";
    }
    echo '</pre>';
    }
    ?>
    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Andy Hassall

      #3
      Re: eva(...)l errors what's the best way to output?

      On 16 Mar 2004 23:52:58 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
      [color=blue]
      >["Followup-To:" header set to comp.lang.php.]
      >Sims wrote:[color=green]
      >> I have an eval(...) and sometimes, (not often), i get an error.
      >>
      >> I can catch the error quite easily,
      >> if( FALSE === eval($code ))
      >> {
      >> // output the code
      >> echo ( htmlentities($c ode );
      >> }
      >>
      >> That does give me the error and the code but how can i get the code to be
      >> nicely lined up, or even have line numbers in front?
      >> Just to make it easier to debug.[/color]
      >
      >Maybe like this? :)
      >
      ><?php
      >$code = 'echo 7*3;
      >echo 7#3; // error! the # makes this the same as "echo 7 echo 7+3"
      >echo 7+3; // this is the line where the error is reported
      >return true;';
      >
      >if (FALSE === eval($code)) {
      > echo '<pre>';
      > $code2 = explode("\n", htmlentities($c ode));
      > for ($i = 1; $i <= count($code2); ++$i) {
      > echo "$i\t{$code 2[$i-1]}\n";
      > }
      > echo '</pre>';
      >}
      >?>[/color]

      Or make it prettier with highlight_strin g?

      <?php
      $code = 'echo 7*3;
      echo 7#3; // error! the # makes this the same as "echo 7 echo 7+3"
      echo 7+3; // this is the line where the error is reported
      return true;';

      if (FALSE === eval($code))
      {
      echo "<hr>";
      $linenum = 0;
      foreach (explode("<br />", highlight_strin g("<?php\n$code \n?>", true)) as
      $line)
      printf("%-6d: %s<br>\n", $linenum++, $line);
      }

      ?>

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

      Comment

      • Sims

        #4
        Re: eva(...)l errors what's the best way to output?

        >[color=blue]
        > Or make it prettier with highlight_strin g?
        >
        > <?php
        > $code = 'echo 7*3;
        > echo 7#3; // error! the # makes this the same as "echo 7 echo 7+3"
        > echo 7+3; // this is the line where the error is reported
        > return true;';
        >
        > if (FALSE === eval($code))
        > {
        > echo "<hr>";
        > $linenum = 0;
        > foreach (explode("<br />", highlight_strin g("<?php\n$code \n?>", true))[/color]
        as[color=blue]
        > $line)
        > printf("%-6d: %s<br>\n", $linenum++, $line);
        > }
        >
        > ?>
        >[/color]

        Thank you both, it works brilliantly.
        Something not really needed but it does make my life a touch easier when
        something does go wrong :)

        Sims


        Comment

        Working...