PHP concatenation error

echo '<li><label><input type="checkbox" name="slb_list[]" value=".'$list->ID'.">'. $list->post_title .'</label></li>';

Error →

Parse error : syntax error, unexpected variable “$list”, expecting “,” or “;” in

Can some guide me where I am faltering? I think the issue is with the wrong concatenation.

Full Code →

<?php 
        global $wpdb;
        $list_query = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = 'slb_list' AND post_status IN ('draft', 'publish')");
        if (!is_null($list_query)) {
          foreach ($list_query as $list) {
            echo '<li><label><input type="checkbox" name="slb_list[]" value=".'$list->ID'.">'. $list->post_title .'</label></li>';
          }
        }
        ?>

Your concatenation is wrong. Look at it closely.

1 Like

(to emphasize the point) look at the coloration of the stuff around your ID concat, compared to the coloration around your post_title concat.

1 Like

value=".$list->ID." this reduced the aprse error, but still the loop is not generating anything.

Does your IDE colorize your code? What does it look like now?

1 Like

Does that look right to you?

1 Like

Is this Ok Now?

You tell me. Is it working?

1 Like

Finally loop is not generating anyting. If I am still wrong please guide me with correct code.

What do you get when you dump $list ?

1 Like

Problem is fixed for now, but please confirm this is concatenation model:

echo 'String 1' . Some PHP code . 'String 2' . Some PHP code . 'String 3';

string concatenation will concatenate any two things that can be strings.
"a" . "b", completely valid, because they’re both strings.
"a" . 3, also valid because an integer can be implicitly cast as a string.
"a". babbit(), might be valid, depending on the return of babbit().

concatenation is an operator, like + or - ; you cant do “a” + “b” (well you can, but it’s a 0 and will belch warnings at you that you tried to use a non-numeric value in a math operation).
Like + requires numeric-able operands, . requires string-able operands.

echo "a" . 3 . max(1,2) ."b";

is a perfectly valid piece of code. Little odd on the eyes, but valid.

1 Like

Still wrong. You need to follow and count how many single quotes and double quotes you have. I know you said you fixed it so that’s good. But this code you now have is being translated to literal $list->ID instead of the value of $list->ID. When you use single quotes, you have to escape the single quotes if you want to use any variable of any sort or it’ll give you the exact thing you gave it.

You were attempting to do that, but incorrectly escaped it which was what I was pointing at.

EDIT: I had typed this in a draft and didn’t see the fixes until now. Your fix is right.

1 Like

On your original issue, if you avoid concatenation altogether, by putting the php variable(s) inside of an overall double-quoted string, you can simplify the syntax by eliminating the extra quotes and extra dots, reducing the chance for mistakes.

echo "<li><label><input type='checkbox' name='slb_list[]' value='$list->ID'>$list->post_title</label></li>";
1 Like

On PHP.net they are also using {} →

// This works:
echo “qwe{$a}rty”; // qwe12345rty, using braces
echo “qwe” . $a . “rty”; // qwe12345rty, concatenation used

The use of this {} operator seems to be simplifying things better than anything else.

Better still, don’t concatenate, just list the items to echo:

echo “qwe”, $a, “rty”;

1 Like

Try using PHP Heredoc to add line feeds and to simplify concatenation:

<?php declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

global $wpdb;

$tmp = <<< ____EOT
  SELECT 
    ID, post_title 
  FROM
   {
    {$wpdb->posts}
   } 
  WHERE 
    post_type = 'slb_list' 
  AND 
    post_status 
  IN 
    (
      'draft', 'publish'
    )
____EOT;
// BEWARE: NO TRAILING SPACES AFTER ;

$list_query = $wpdb->get_results( $tmp );
  

if ( ! is_null($list_query) )
{

  foreach ($list_query as $list)
  {
    $tmp = <<< ____EOT
      <li>
        <label>
          <input 
            type  = "checkbox" 
            name  = "{slb_list[]}" 
            value = "{$list->ID}
          >
           {$list->post_title} 
        </label>
      </li>
____EOT;
// BEWARE: NO TRAILING SPACES AFTER ;

    echo  $tmp;
  }//endforeach
}

Edit:

Spot the deliberate mistake :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.