Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count() causing "unexpected T_STRING" error?

Tags:

php

wordpress

I'm trying to define a new shortcode in WordPress, and I get the following error when the function is loaded (just loaded, I haven't tried to call it anywhere yet):

Parse error: syntax error, unexpected T_STRING in /pathtomytheme/user_functions.php on line 105

Here's the code; line 105 is "$cat_n = count($cats) – 1;":

function usertag_2colcats($atts) {
extract(shortcode_atts(array('parent' => 0,'depth' => 2,), $atts));
$cats = explode('<br />', wp_list_categories('title_li=&echo=0&depth=' . $depth . '&style=none&show_count=1&use_desc_for_title=0&child_of=' . $parent));
$cat_n = count($cats) – 1;
for ($i = 0; $i < $cat_n; $i++) {
    if ($i < $cat_n/2) $cat_left = $cat_left . '<li>' . $cats[$i] . '</li>';
    elseif ($i >= $cat_n/2) $cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
}
echo '<ul class="leftcats">' . $cat_left . '</ul><ul class="rightcats">' . $cat_right . '</ul>';

}

If I change that line so that it doesn't use the count function, e.g. to "$cat_n = 5;", the function loads without error. It seems like I'm missing something really obvious; what is it?

The original code is here: http://pcsplace.com/blog-tips/how-to-split-categories-list-into-columns-in-wordpress/

like image 290
Becca Avatar asked Nov 15 '25 23:11

Becca


2 Answers

This might sound weird, but the "-" sign in line 105 is a weird character. Try to rewrite that line at hand instead of copying and paste it. I did it, and the error went away.

Edit: Ok, so this is what I found. The character you have in line 105 has ASCII code 226. But the ASCII code for the minus sign is 45. So definitely your problem is there.

Avoid copy paste at all cost ;)

like image 133
rogeriopvl Avatar answered Nov 17 '25 17:11

rogeriopvl


Have you tried to var_dump( $cats ) ?

Sometimes count() can return false depending on some situations, but on this case I'm pretty sure it only returned the entire string as it didn't found it.

like image 32
Erick Avatar answered Nov 17 '25 18:11

Erick