简体   繁体   中英

Printing php variables in html

I've been tying myself in knots a bit trying to figure out how to print a couple of PHP variables into some HTML. I just can't seem to get the single or double quotes correct.

I am pulling images from the 500px API and I want to render them into a bootstrap carousel. The variables I want to use are $photo->name (which I have put in between the heading tags below and $photo->url which I want to put in instead of the placehold.it url. Everything I have tried gives me an Parse error: syntax error, unexpected T_VARIABLE error however.

Can anyone help?

<?php           
      $i = 0;      
      $len = count($obj);
      foreach ($obj->photos as $photo){

        if ($i==0) {
          print '<div class="item active">
                    <div class="fill" style="background-image:url('http://placehold.it/1900x1080&amp;text=Slide One');">
                    </div>
                  <div class="carousel-caption">
                    <h1>$photo->name</h1>
                  </div>
                  </div>';
        } else {
          print "hello 2";
        }
        $i++;
      }
  ?>

There are several ways, using "echo", including:

$world = "world";

#If you wrap your statement in double-quotes, then:
echo "Hello $world, nice to meet you.";

#If you wrap your echo statement in single-quotes, then:
echo 'Hello '.$world.', nice to meet you.';

So, you might consider changing your code to:

<?php 

  $url = 'http://placehold.it/1900x1080&amp;text=Slide One';
  $i = 0;      
  $len = count($obj);

  foreach ($obj->photos as $photo){

    if ($i==0) {

      print '<div class="item active">
                <div class="fill" style="background-image:url(\''.$url.'\');">
                </div>
              <div class="carousel-caption">
                <h1>'.$photo->name.'</h1>
              </div>
              </div>';

    } else {

      print "hello 2";

    }

    $i++;

  }

?>

Try this

 <?php           
          $i = 0;      
          $len = count($obj);
          foreach ($obj->photos as $photo){

            if ($i==0) {
              print '<div class="item active">
                        <div class="fill" style="background-image:url(\'http://placehold.it/1900x1080&amp;text=Slide One\');">
                        </div>
                      <div class="carousel-caption">
                        <h1>'.$photo->name.'</h1>
                      </div>
                      </div>';
            } else {
              print "hello 2";
            }
            $i++;
          }
      ?>
..."background-image:url(\'http://placehold.it/1900x1080&amp;text=Slide One\')"...

使用\\逃逸

Use echo and paste html5 code between double quotes. Example:

echo "<div id='mydiv'>$phpvar</div>";

or concatenate vars with dots:

    echo "<div id='mydiv'>".$phpvar."</div>";

Sometimes the error depends on single or double quotes.


Have fun! (:

You are having an escaping issue with strings, please observe the following examples:

$variable = 'my string';

echo $variable ; // my string
echo '$variable '; // $variable
echo "$variable "; // my string

$variable = 'my st'ring'; // ERROR
$variable = 'my st\'ring'; // escaped properly

echo $variable ; // my st'ring
echo '$variable '; // $variable
echo "$variable "; // my st'ring
<?php           
      $i = 0;      
      $len = count($obj);
      foreach ($obj->photos as $photo){

        if ($i==0) {
          print "<div class='item active'>
                    <div class='fill' style='background-image:url(\"http://placehold.it/1900x1080&amp;text=Slide One\");'>
                    </div>
                  <div class='carousel-caption'>
                    <h1>$photo->name</h1>
                  </div>
                  </div>";
        } else {
          print "hello 2";
        }
        $i++;
      }
?>

If you want to add a variable to print or echo, you must close the quotes and add a point at the start and the end. For example:

<?php
     $and = "and";
     echo "Hello ".$and." how are you?"; //prints Hello and how are you
?>

edit: not I noticed that you have a quotes between url() . The PHP thinks that the string stops there. You must add a left-slash: \\ before it if you want that the next letter after it will be a part of the string. for example:

<?php
    echo "My teacher said "You are not listening!"."; // Even SO's editor thinks that "You are not listening!" isn't a part of the string and adds a color. It will return an erro.
    // the right way is:
    echo "My teacher said \"You are not listening!\"."; // Here you can see that the editor doesn't adds color to whatever in the quotes because it know that is is a part of the string. It prints My teacher said "You are not listening!".
?>

in your case:

<?php           
      $i = 0;      
      $len = count($obj);
      foreach ($obj->photos as $photo){

        if ($i==0) {
          print '<div class="item active">
                    <div class="fill" style="background-image:url(\'http://placehold.it/1900x1080&amp;text=Slide One\');">
                    </div>
                  <div class="carousel-caption">
                    <h1>'.$photo->name.'</h1>
                  </div>
                  </div>';
        } else {
          print "hello 2";
        }
        $i++;
      }
  ?>

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