SMF PHP error... Am I missing something subtle in the PHP languagesyntax?

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

    SMF PHP error... Am I missing something subtle in the PHP languagesyntax?

    Given these two code snippets, the first generates errors, the second
    doesn't...

    // generates errors
    $tboard = $context['linktree'][2]['name'];
    $tboard = empty($tboard)? '':' - '.$tboard;
    $ttopic = $context['linktree'][3]['name'];
    $ttopic = empty($ttopic)? '':' - '.$ttopic;
    if (! ereg($context['forum_name'], $context['page_title']))
    $context['page_title'] = $context['linktree'][0]['name'].$tboard.
    $ttopic;

    // no errors
    $tboard = empty($context['linktree'][2]['name'])?'':' - '.
    $context['linktree'][2]['name'];
    $ttopic = empty($context['linktree'][3]['name'])?'':' - '.
    $context['linktree'][3]['name'];
    if (! ereg($context['forum_name'], $context['page_title']))
    $context['page_title'] = $context['linktree'][0]['name'].$tboard.
    $ttopic;


    Here's a few sample errors that I see in my SMF log only when using
    the first version of the code. I think the "2" and "3" referred to
    are the subscripts in the code snippet...


    8: Undefined offset: 3
    File: /home/xxxxxx/public_html/forums/Themes/default/
    Admin.template. php (main_above sub template - eval?)
    Line: 66

    8: Undefined offset: 2
    File: /home/xxxxxx/public_html/forums/Themes/default/
    Admin.template. php (main_above sub template - eval?)
    Line: 64


    For the life of me, I cannot figure out the first block of code
    generates errors. Am I missing something subtle about the PHP syntax?
  • =?iso-8859-1?Q?=C1lvaro?= G. Vicario

    #2
    Re: SMF PHP error... Am I missing something subtle in the PHP language syntax?

    *** William Krick escribió/wrote (Mon, 29 Sep 2008 11:49:14 -0700 (PDT)):
    // generates errors
    $tboard = $context['linktree'][2]['name'];
    $tboard = empty($tboard)? '':' - '.$tboard;
    // no errors
    $tboard = empty($context['linktree'][2]['name'])?'':' - '.
    $context['linktree'][2]['name'];
    The key point is that empty($foo) does not trigger a notice when $foo is
    not set.

    --
    -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    -- Mi sitio sobre programación web: http://bits.demogracia.com
    -- Mi web de humor en cubitos: http://www.demogracia.com
    --

    Comment

    • William  Krick

      #3
      Re: SMF PHP error... Am I missing something subtle in the PHPlanguage syntax?

      On Sep 29, 3:42 pm, "Álvaro G. Vicario"
      <webmasterNOSPA MTHA...@demogra cia.comwrote:
      *** William Krick escribió/wrote (Mon, 29 Sep 2008 11:49:14 -0700 (PDT)):
      >
      // generates errors
      $tboard = $context['linktree'][2]['name'];
      $tboard = empty($tboard)? '':' - '.$tboard;
      // no errors
      $tboard = empty($context['linktree'][2]['name'])?'':' - '.
      $context['linktree'][2]['name'];
      >
      The key point is that empty($foo) does not trigger a notice when $foo is
      not set.
      >
      Doesn't the first line in the example guarantee that $tboard is set,
      even if to null or an empty string?

      Basically, I just want to alias $context['linktree'][2]['name']
      to a variable so that the code is clearer and easier to follow.
      The only snag is that $context['linktree'][2]['name']
      may sometimes be blank.

      This shouldn't be this difficult to do.

      Comment

      • Tim Roberts

        #4
        Re: SMF PHP error... Am I missing something subtle in the PHP language syntax?

        William Krick <wkrick@gmail.c omwrote:
        >On Sep 29, 3:42 pm, "Álvaro G. Vicario"
        ><webmasterNOSP AMTHA...@demogr acia.comwrote:
        >*** William Krick escribió/wrote (Mon, 29 Sep 2008 11:49:14 -0700 (PDT)):
        >>
        // generates errors
        $tboard = $context['linktree'][2]['name'];
        $tboard = empty($tboard)? '':' - '.$tboard;
        // no errors
        $tboard = empty($context['linktree'][2]['name'])?'':' - '.
        $context['linktree'][2]['name'];
        >>
        >The key point is that empty($foo) does not trigger a notice when $foo is
        >not set.
        >>
        >
        >Doesn't the first line in the example guarantee that $tboard is set,
        >even if to null or an empty string?
        Yes, but you'll get a warning in the first case if $context['linktree']
        does not contain [2]. The "empty" function suppresses that warning.
        >Basically, I just want to alias $context['linktree'][2]['name']
        >to a variable so that the code is clearer and easier to follow.
        >The only snag is that $context['linktree'][2]['name']
        >may sometimes be blank.
        ....or missing, apparently. That's the problem.
        --
        Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • William  Krick

          #5
          Re: SMF PHP error... Am I missing something subtle in the PHPlanguage syntax?

          On Oct 5, 12:52 am, Tim Roberts <t...@probo.com wrote:
          William  Krick <wkr...@gmail.c omwrote:
          >
          On Sep 29, 3:42 pm, "Álvaro G. Vicario"
          <webmasterNOSPA MTHA...@demogra cia.comwrote:
          *** William Krick escribió/wrote (Mon, 29 Sep 2008 11:49:14 -0700 (PDT)):
          >
          // generates errors
          $tboard = $context['linktree'][2]['name'];
          $tboard = empty($tboard)? '':' - '.$tboard;
          // no errors
          $tboard = empty($context['linktree'][2]['name'])?'':' - '.
          $context['linktree'][2]['name'];
          >
          The key point is that empty($foo) does not trigger a notice when $foo is
          not set.
          >
          Doesn't the first line in the example guarantee that $tboard is set,
          even if to null or an empty string?
          >
          Yes, but you'll get a warning in the first case if $context['linktree']
          does not contain [2].  The "empty" function suppresses that warning.
          >
          Basically, I just want to alias  $context['linktree'][2]['name']
          to a variable so that the code is clearer and easier to follow.
          The only snag is that  $context['linktree'][2]['name']
          may sometimes be blank.
          >
          ...or missing, apparently.  That's the problem.

          Hmm... so then the second example might not be doing what I want, even
          if there is no error.

          Any suggestions on the "right" way to do what I'm trying to do?

          Do I need to first test using isset(), then empty(), or possibly try
          to determine the length of $context['linktree'] before taking any
          action?

          Essentially, there are potentially 4 items in the linktree...

          $a = $context['linktree'][0]['name']
          $b = $context['linktree'][1]['name']
          $c = $context['linktree'][2]['name']
          $d = $context['linktree'][3]['name']

          ....the presence of each item depends on the one before it, so if $c is
          present, $a and $b will also be present. I want is this...

          $context['page_title'] = $a.' - '.$c.' - '.$d; (if all three are
          present)
          $context['page_title'] = $a.' - '.$c.'; (if two are present)
          $context['page_title'] = $a.'; (if one is present)

          ....I only want the dashes to show up if the next item exists,
          otherwise, I end up with a lot of extra dashes at the end of the page
          title when I'm further up the tree. Hence my original logic with
          empty().

          Any suggestions are appreciated.

          Comment

          Working...