count() causing "unexpected T_STRING" error? - php

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/

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 ;)

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.

Related

Migration to PHP8

I have code from old PHP.
But when I tried to execute it by PHP 8.
The first code was:
PasteBin
I had error:
Fatal error: Array and string offset access syntax with curly braces
is no longer supported in **** on line 550
On line:
for ($i = 0; $i < strlen($text); $i++) $res .= ord($text{$i}) . "-";
I changed it to:
for ($i = 0; $i < strlen($text); $i++) $res .= ord($text[$i]) . "-";
But I had another error:
Warning: Trying to access array offset on value of type bool in ***
on line 76
On line:
$real = $row['sip'];
I have no idea - how to rewrite this string.
Can you help me?
the problem is that you are trying to access a boolean value as you do with an array.
i imagine that $row is the result of a query, and that query does not return any matching rows, so it's false.
just check if $row is false before you access it.
<?php
$row = false;
echo $row['test'];
this returns that warning.
as per your comment, it depends on what you wanna do.
if it exist return the values, if it doesn't?
if($row){
// if it contains something, do something with it
}else{
// do something else if it doesn't
}
i don't know what's the flow of your code, so I can't really help you, it's just a check to see if the $row variable is not false

Carriage return PHP multi line

I'd like to do a multi line carriage return with php for the command line.
Is this possible?
I know already how to achieve it with one line print("\r") but it like to know how to do this on multiple lines.
The printed data should look like this:
$output = "
Total time passed: 34, \n
Total tests: 14/523
";
print($output . "\r");
It works for a single line so that there is not a new line added every time it is printed in the loop.
When i use it with a PHP_EOL, or \n i'm getting new lines all the time. I just want two lines to show up while updating it.
You can do this using ANSI escape
char [F
As example:
for ($i = 1; $i < 5; $i++) {
$prevLineChar = "\e[F";
sleep(1);
echo "$i\nof 5\n" . $prevLineChar . $prevLineChar;
}
You are looking for PHP_EOL, one for each carriage return. See here: https://stackoverflow.com/a/128564/2429318 for a nice friendly description.
It is one of the Core Predefined Constants in PHP
PHP_EOL (string)
The correct 'End Of Line' symbol for this platform. Available since PHP 5.0.2
You could write something like:
print("\n\n\n\n\n");
but what if you want to print a dynamic number of new lines :)
try this function:
function new_lines($n) {
for($i = 0; $i < $n; $i++) { echo PHP_EOL; }
}
call it like so :
//print 100 new lines
new_lines(100);

PHP Fatal error: Unsupported operand types in For Loop

I got an Error in my For Loop for some reason.
I just want to go backwards through an array in php.
The Error in my Browser is:
Fatal error: Unsupported operand types in /var/html/modules/getChat.php on line 18
Line 18 in this Code part line 1:
Here is the Code:
for($x = sizeof($result-1); $x > 0; $x--)
{
echo '<div class="message '.$result[$x].'"> <img src="'.$result[$x].'" /><span class="name">'.$result[$x].'</span>
<p>'.$result[$x].'</p>
</div>';
}
I hope you can help
Thanks
$result is an array, and substracting 1 from an array doesn't make any sense. You probably wanted to use this instead:
for ($x = sizeof($result) - 1; $x > 0; $x--) // ...
And yes, it seems that you unintentionally skip the very first element of your array here. If so, fix the condition ($x >= 0) - or just compact the whole loop into while:
$x = count($result);
while($x--) {
// output with $result[$x]
}
If that's not a bottleneck (and most probably it's not), you better show the real intent of the code with array_reverse:
foreach (array_reverse($result) as $el) {
// output with $el
}

php undefined offset 150 error in for loop

I keep getting an undefined offset 150 error, but i am not sure what it means or what i should do to debug it. Do to the error line i believe that it has something to do with my for loop.
// Get Datafile
$MyData = file("./tmp/test.txt");
// Get Column Headers
$ColHeads = explode(" ", $MyData[1]);
unset($MyData[1]);
$LastHeader = "";
for ($i = 0;$i <= count($ColHeads);$i++) {
if ($ColHeads[$i] == $LastHeader) { //<---this is the line that errors
$ColHeads[$i] = $ColHeads[$i] . "1";
}
$LastHeader = $ColHeads[$i];
}
Would anyone have any ideas as to where I am going wrong?
and the error is:
Undefined offset: 150
I am sorry if this is vague. I am not familiar with php and not sure where to start on debugging this... any help would be much appreciated! Thank you!
Change your for loop:
for ($i = 0;$i < count($ColHeads);$i++) {
The problem is that on the last iteration of the loop, when $i == count($ColHeads), that's going to set $i too high for the number of items in $ColHeads.
You started off right by setting $i = 0. If $ColHeads has 5 items in it, those items have indexes from 0 to 4. Your for loop, as it is, goes up to 5 - and there is no $Colheads[5], so the error is thrown.
Array index starts from zero. And ends at Length-1. So it should be:
for ($i = 0;$i < count($ColHeads);$i++) {

What is the proper PHP way to include variable_1, variable_2, variable_3 when enclosed between ' '

I received the following error for a while-loop:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING
$index=1;
while ($index <= 100):
fwrite($outfile, $_POST[\'"variable_" . $index\']);
fwrite($outfile, "\r");
$index = $index + 1;
endwhile;
fclose($outfile);
?>
What is the proper way to include variable_1, variable_2, variable_3 without getting syntax error?
Thanks.
$x = "variable_$index";
fwrite($outfile, $_POST[$x]);
I'd consider using a for loop here as well.
// Assuming you open $outfile somewhere prior to this
for ( $i = 1; $i <= 100; $i++ ) {
$x = "variable_$i";
fwrite($outfile, $_POST[$x]);
fwrite($outfile, "\r");
}
fclose($outfile);
Looks like you have your quotes mixed up - you don't need the \' parts:
$_POST["variable_" . $index]
there was mistake in single and double quotes
fwrite($outfile, $_POST["'variable_" . $index."'"]);
fwrite($outfile, implode("\r", array_values($_POST)));

Categories