JeffInho Posted January 1, 2016 Share Posted January 1, 2016 (edited) Hi Guys, maybe someone can brighten up my day here.I don't php at all, and still trying to do modifications on my own I have this piece of code <span class="updated"><?php the_time('j F Y'); ?> <span class="updated"><?php $u_date = get_the_time('j F Y'); $u_modified_date = get_the_modified_date('j F Y'); if ($u_modified_date !== $u_date) { echo "and last modified on "; the_modified_date('j F Y'); } ?> this script works and displays the proper thing, but I want <span class="updated"> to be conditional too.Either it's on the_time, or if there's a the_modified_date falling into that clause, it should only apply to the_modified_date and not to the entire string (right the "updated" with this code shows the_time + and last modified on + the_modified_date every time I tried to include <span class="updated"> in a if clause, it totally broke my page and I have no idea how to solve this.Would someone be kind enough to point me to the right direction ?thanks! Edited January 1, 2016 by gizmola Quote Link to comment https://forums.phpfreaks.com/topic/300084-total-noob-need-help-with-basic-if-condition/ Share on other sites More sharing options...
ginerjm Posted January 1, 2016 Share Posted January 1, 2016 If you are REALLY going to try and learn php the first thing would be to learn how to keep your html code separate from the php code. Makes things easier to code and to maintain and to read. Calculate all your values into php vars and then insert them into the html code where appropriate instead of switching in and out of php mode to do the calcs. A sample of variable insertion would be: echo "The time is $time_no"; Note the use of the double quotes to wrap the string and the var in. Single quotes won't work on a variable. Try doing the calcs first then do your output. Quote Link to comment https://forums.phpfreaks.com/topic/300084-total-noob-need-help-with-basic-if-condition/#findComment-1528929 Share on other sites More sharing options...
gizmola Posted January 1, 2016 Share Posted January 1, 2016 I don't think ginerjm's advice is wrong, but I can also offer you this which might help. One of the great things about PHP is the way you are able to intermix PHP and HTML blocks as you are doing in your code. It is also one of the things that makes code messy and looking like spaghetti for novices. In your case, you also are not properly closing your spans. It should be: <span> ..... </span> With and if-then-else block there are a few different ways of handling this. Do it all in PHP, echoing out results Simplify within PHP using the ternary operator http://robertsettle.com/2012/07/php-shorthand-if-statements/ Intermix PHP and HTML by dropping in and out of PHP Intermix PHP and HTML by dropping in and out of PHP with the Alternative syntax http://php.net/manual/en/control-structures.alternative-syntax.php If you want to truly learn the answer, then you need to do some reading about these features. Here's something to ponder, which hopefully will cause you to investigate the materials I referenced, not to mention the underlying PHP features. //html mode <span class="updated"><?php the_time('j F Y'); ?></span> <?php $u_date = get_the_time('j F Y'); $u_modified_date = get_the_modified_date('j F Y'); ?> <?php if ($u_modified_date !== $u_date): ?> <span class="updated">and last modified on <?php echo the_modified_date('j F Y'); ?> </span> <?php endif; ?> No matter how you approach this, the important thing is to have clean, easy to read, easy to separate and understand code, using blocks and indentation. Quote Link to comment https://forums.phpfreaks.com/topic/300084-total-noob-need-help-with-basic-if-condition/#findComment-1528952 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.