taltal13 Posted September 30, 2018 Share Posted September 30, 2018 EXTREME Noobie here - as in I first tried PHP yesterday. I'm trying to change the image based on what date it is. I started with this but not getting anywhere: Looking forward to hearing, learning and posting :) <?php echo "Today is " . date('l F, jS Y. '); ?> <?php $b = date(S); if ($b % 2 == 0) { echo '<img src="Barboncino.png" />'; } else { echo '<img src="Not_Barboncino.png" />'; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2018 Share Posted September 30, 2018 Have you tried echo $b; to see what it's value is? Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 30, 2018 Share Posted September 30, 2018 As a fresh noob one of the very first things you need to learn before writing any code is how to turn on and display errors. If you had done this PHP would have told you what is wrong. Quote Link to comment Share on other sites More sharing options...
kicken Posted October 1, 2018 Share Posted October 1, 2018 11 hours ago, taltal13 said: $b = date(S); That line of code is incorrect. First, string arguments need to be quoted. Having just S like that attempts to refer to a constant named S which doesn't exist. Currently PHP will assume you mean "S" and emit a warning then continue on, but future versions of PHP will instead emit an error and stop. Second, if you look up the documentation for date and see what the identifier S means, you'll see that it refers to "English ordinal suffix for the day of the month, 2 characters", ie (st, nd, rd or th). That value is not a number and as such isn't going to provide an meaningful result when used with the modulus operator (%). What you probably want is the numeric day of the month, which is provided by the identifier j. $dayOfMonth = date('j'); if ($dayOfMonth % 2 == 0){ //do something } else { //do something else } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 1, 2018 Share Posted October 1, 2018 Besides the helpful responses above another is: One doesn't need to enter and exit php mode for each little block of php code. You have a long way to go but if you do a bit of reading on the essentials of php you will move along a lot quicker. Google a php learning site and take a couple of lessons on proper syntax and the simpler things of writing code, such as recognizing the difference between "strings" and "variables". And since it wasn't provided when mentioned, this is what you should use at the top of every php script. error_reporting(E_ALL | E_NOTICE); // types of errors to show ini_set('display_errors', '1'); // turn on client display (set to '0' to disable) ini_set('log_errors','1'); // turn on log writing set_time_limit(2); // limit the time a script can run erroneously. You should turn this off or remove once you are ready to put the script into production. Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 1, 2018 Share Posted October 1, 2018 Quote And since it wasn't provided when mentioned, this is what you should use at the top of every php script. While that indeed will work, you have now littered your code base with redundant error settings that will have to be edited on every page when you upload to production. The "better" method would be to set those setting one time in one place in the php.ini on your local dev setup. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 1, 2018 Share Posted October 1, 2018 2 minutes ago, benanamen said: While that indeed will work, Questionable - it will not report syntax errors. The settings must be in the ini file for that. Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 1, 2018 Share Posted October 1, 2018 Yeah, that's what I get for being more concerned about coming across as reproving an established forum member. The point, as you rightly support is that error reporting should be set in the ini file. Quote Link to comment 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.