Jump to content

Syntax issues, still a newbie but getting there!


Go to solution Solved by Barand,

Recommended Posts

Good day everyone!

 

I was wondering how one could get this snippet of code to echo correctly please.

Please review code below.

I have tried to escape this puppy in many ways and cannot figure it out.

Could someone assist me?

 

 

Thank you kindly,

 

Crazycabling

<?php
  if ($ticket['status'] == 'Fermé')
  {
     echo "<div class=\"btns\">";

     // I would need this part to echo the button correctly if above status is Fermé

     <a href="viewtest.php?id=<?=$_GET['id']?>&status=Réouvert" class="btn blue" style="width:400px">Réouvrir ce billet</a>

     //End of part

     echo "</div>";
     echo 'billet fermé';
   }
?>

 

  • Solution

HTML code wil not be output when it is inside <?php .. ?> tags. It need to be output using echo command.

<?php
  if ($ticket['status'] == 'Fermé')
  {
     echo "<div class=\"btns\">
           <a href='viewtest.php?id={$_GET['id']}&status=Réouvert' class='btn blue' style='width:400px'>Réouvrir ce billet</a>
           </div>
          ";
     echo 'billet fermé';
   }
?>

The alternative is exit php mode, output the html then re-enter php mode, which gets very messy...

<?php
  if ($ticket['status'] == 'Fermé')
  {
     echo "<div class=\"btns\">";

     // I would need this part to echo the button correctly if above status is Fermé
?>
     <a href="viewtest.php?id=<?=$_GET['id']?>&status=Réouvert" class="btn blue" style="width:400px">Réouvrir ce billet</a>
<?php
     //End of part

     echo "</div>";
     echo 'billet fermé';
   }
?>

 

In general, if you have a static string with double quotes inside it, use single quotes.  It's a micro optimization, but a good habit to start with, that you should always try to use single quotes, as they don't interpolate variables.  If you don't need interpolation then go with single quotes.

echo '<div class="btns">';

The problem with the code you presented is that you are inside a php block, so there is no reason for you to once again try and start a php block.  It's not clear if you omitted anything however.  If you were outside a PHP block and in "HTML" mode, then your code would work if you had simply echoed it.

Since you are using alternative syntax that should work, but of course it will only work if the variable $_GET['id'] is not empty.

To interpolate an array variable with single quotes, you simply need to enclose that entire variable inside curly brackets, which in essence tells php to perform evaluation of that variable and not treat it as a string that might require interpolation.

However, in this case you also have a PHP string, so really all you want to do here in most cases is just use string concatenation.

echo '<a href="viewtest.php?id=' . $_GET['id'] . '&status=Réouvert" class="btn blue" style="width:400px">Réouvrir ce billet</a>';

To use a heredoc

echo <<<EOT
<a href="viewtest.php?id={$_GET['id']}&status=Réouvert" class="btn blue" style="width:400px">Réouvrir ce billet</a>
EOT;

heredocs and nowdocs can be very useful when you have substantial amounts of html and php to be interpolated, although for one or two lines in mostly php code I probably wouldn't use it.

I'll add my .02 cents to the discussion.

 

What I like do is to design the web page first and if I know something is going to repeat itself (though it's also good for non-repeating code as well) then I just add the PHP to the HTML when I am coding it.

As an example a blog or a page that will have more than one article to the page ->

    <?php
    foreach ($cms as $record) {

        echo '<header class="sectionHeader">';
        echo '<h2 class="sectionHeadingText">'. $record['heading'] . '</h2>';
        echo '</header>';
        echo '<article class="sectionArticle">';
        echo '<img class="imageArticle right-side" src="' . $record['image_path'] . '" alt="LEGO Bookstore">';
        echo '<p class="articleText">' . nl2br($record['content']). '</p>';
        echo ' </article>';
        echo '<footer class="sectionFooter">';
        echo '<p class="sectionText">Written by ' . $record['author'] . ' on ' . CMS::styleDate($record['date_added']) .' </p>';
        echo '</footer>';
    }
    ?>

I would start off with straight HTML then add the PHP to the page. I personally like using echo as I can easily modify certain HTML elements easier, but that is just probably me. 😀

Edited by Strider64
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.