using ' and ” in php syntax [duplicate]

Possible Duplicate:
How to get useful error messages in PHP?

Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed’s site.

I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers

<?php 
  // F1 W/H xml feed
  $xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');

  foreach ($xml->response->williamhill->class->type as $type) {
    $type_attrib = $type->attributes();
    echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>


<ul>
  <?php 
      foreach($type->market as $event) {
          echo "<li>";
          echo "<a href="$event_attributes['url']">";
          echo $event_attributes['name'];
          echo "</a>";
          echo "</li>";
      }
  ?>
</ul>

Comments 6

  • echo "<a href="$event_attributes['url']">";
    

    try changing that line to

    echo "<a href="".$event_attributes['url']."">";
    

    The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.

    $hello = "Hello";
    echo "$hello master";
    

    is the same as

    $hello ="Hello";
    echo $hello.' master';
    
  • if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax

    with the given code it would look like

    <?php 
    // F1 W/H xml feed
    $xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
    
    foreach ($xml->response->williamhill->class->type as $type) {
        $type_attrib = $type->attributes();
        echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
    } ?>
    
    <ul>
        <?php foreach($type->market as $event):?>
            <li>
                <a href="<?php echo $event_attributes['url']; ?>">
                    <?php echo $event_attributes['name']; ?>
                </a>
            </li>
        <? endforeach;?>
    </ul>
    

    one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours

  • In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:

    echo "<a href="".$event_attributes['url']."">";
    

    Instead of

    echo "<a href="$event_attributes['url']">";
    

    Your example throws and error because you haven’t used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you’d need to add the double quotes according to html standard. Hence you have to double quote.

  • When you are testing your PHP scripts, you’ll find it useful to switch on errors – then PHP will actually tell you why it isn’t showing you anything:

    error_reporting(E_ALL);
    

    Normally you will have missed a ; or mis-typed a variable name.

    in your case the error is here:

    echo "<a href="$event_attributes['url']">";
    

    You have accidentally ended the string with a double quote, so PHP thinks the string ends here:

    echo "<a href="
    

    This is where using single-quotes can be very handy because your double quotes won’t then close the string.

    echo '<a href="' . $event_attributes['url'] . '">';
    

    The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn’t. For example:

    $myVar = "BLAH";
    echo "Example $myVar"; // Example BLAH
    echo 'Example $myVar'; // Example $myVar
    
  • You are missing the periods in your second echo, where you have your $event_attributes['url']

    <?php 
      foreach($type->market as $event) {
          echo "<li>";
          echo "<a href=".$event_attributes['url'].">";
          echo $event_attributes['name'];
          echo "</a>";
          echo "</li>";
      }
    

    ?>

    I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.

  • Change

    echo "<a href="$event_attributes['url']">";
    

    for

    echo "<a href=".$event_attributes['url'].">";
    

发表评论

电子邮件地址不会被公开。 必填项已用*标注