Crazycabling Posted February 15, 2023 Share Posted February 15, 2023 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é'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315919-syntax-issues-still-a-newbie-but-getting-there/ Share on other sites More sharing options...
Solution Barand Posted February 15, 2023 Solution Share Posted February 15, 2023 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é'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315919-syntax-issues-still-a-newbie-but-getting-there/#findComment-1605730 Share on other sites More sharing options...
gizmola Posted February 15, 2023 Share Posted February 15, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315919-syntax-issues-still-a-newbie-but-getting-there/#findComment-1605731 Share on other sites More sharing options...
Crazycabling Posted February 15, 2023 Author Share Posted February 15, 2023 Thank you!!! I was on the right track at some point but my double quotes betrayed me! It is working now and I understand my errors Thank you again! Crazycabling 1 Quote Link to comment https://forums.phpfreaks.com/topic/315919-syntax-issues-still-a-newbie-but-getting-there/#findComment-1605733 Share on other sites More sharing options...
Strider64 Posted February 16, 2023 Share Posted February 16, 2023 (edited) 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 February 16, 2023 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/315919-syntax-issues-still-a-newbie-but-getting-there/#findComment-1605741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.