Jump to content

Return image after If statement


taltal13

Recommended Posts

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" />';
}
?>

 

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

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.
 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.