简体   繁体   中英

How do I use single quotes inside single quotes php?

Can anyone explain how to make this code work?

echo '<li class="list-clients__item">
      <img src="<?php get_field('brand_logo'); ?>" height="75" width="168" alt="'.$category->cat_name.'"> 
      <span class="helper"></span> 
      </li>';

You have nested php tags inside a php function call.

<?php get_field('brand_logo'); ?>

This is to execute php within dynamic html (eg .phtml).

Stay with dynamic html

<li class="list-clients__item"> 
  <img src="<?php echo get_field('brand_logo'); ?>" height="75" width="168" alt="<?php echo $category->cat_name; ?>">
  <span class="helper"></span> 
</li>

or remove the nesting php tags

echo '<li class=\"list-clients__item\"> 
        <img src=\"' . get_field('brand_logo') . '\" height=\"75\" width=\"168\" alt=\"' . $category->cat_name . '\"> 
        <span class=\"helper\"></span> 
      </li>';

试试这个:

echo '<li class="list-clients__item"> <img src="'. get_field('brand_logo', 'category_' . $category->cat_ID) .'" height="75" width="168" alt="'.$category->cat_name.'"> <span class="helper"></span> </li>';

To make your code work, remove the php tag.

Also to excape the quotes you use the \\ (escape character).

ex. echo 'tom\\'s rest'

echo '<li class="list-clients__item">
      <img src="'.get_field('brand_logo') .'" height="75" width="168" alt="'.$category->cat_name.'"> 
      <span class="helper"></span> 
      </li>';

Whenever I have to embed a bunch of variables and functions into a longish string (of HTML), I find using the following format easier to read:

$brand_logo = get_field('brand_logo');
echo <<<EOF
<li class="list-clients__item">
    <img src="{$brand_logo}" height="75" width="168" alt="{$category->cat_name}">
    <span class="helper"></span>
</li>
EOF;

It's called the heredoc format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM