By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,348 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,348 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Sep 29 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
*** 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
--
Sep 29 '08 #2

P: n/a
On Sep 29, 3:42*pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.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.
Oct 3 '08 #3

P: n/a
William Krick <wk****@gmail.comwrote:
>On Sep 29, 3:42*pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.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, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 5 '08 #4

P: n/a
On Oct 5, 12:52*am, Tim Roberts <t...@probo.comwrote:
William *Krick <wkr...@gmail.comwrote:
On Sep 29, 3:42*pm, "Álvaro G. Vicario"
<webmasterNOSPAMTHA...@demogracia.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.
Oct 6 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.