If statements

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I have a header file that has a code like this

<img src="<?php echo $bg ?>">

On staff.php I have $bg defined as images/headimage.gif

Some pages I don't have $bg defined. Is there a way I can write an if statement that will make $bg = images/default.gif if there is no $bg defined in the page?

Thanks a lot

He has: 698 posts

Joined: Jul 2005

Yep, do something like this:

<?php
if ($bg == "") {
$bg = "whatever";
}
?>
'
There are other ways to determine if a variable is empty, but that's what I always do. Wink

Kurtis

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Can you explain that a little? Where would the default url bg go (http://localhost/images/default.gif) and the variable inside staff.php?

I am new to PHP, I just used includes before.

He has: 698 posts

Joined: Jul 2005

Okay, what you can do is add the code I gave you to each page, and on the pages where $bg should be defined, add this line before the code above:

<?php
$bg
= "whatever";
?>
'

This way it will use whatever that line says, since $bg is not empty. Wink

Kurtis

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Thanks mscreashuns, but I don't think that is what I am going for.

Let me rephrase...

If I define $bg on the staff.php, the background (bg) image will be displayed on staff.php by the header file where the bg tag is. Now, if I do not define $bg I want it to default to localhost/images/bg.gif. I want that code in the header. The site I am working on has 400+ pages and I can't go into everyone and place a code there.

Does that make sense?

This is how it would look in my own language.

if {bg = "anything"
then {make it the background image}

else {make the background = localhost/images/defaultbg.gif}

Laughing out loud

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

What about something like this?

<?php
// if string length is zero, or does not exist
if(strlen($bg) == \"0\") {
 
$bg = \"images/default.gif\";
}
?>

This is more or less what mscreashuns has posted. And you might want to get a bit more complicated and check if the image your changing exists. If it doesn't, then go to default.

<?php
// if string length is zero, does not exist, or file doesn't exist
if(!file_exists($bg) || strlen($bg) == \"0\") {
 
$bg = \"images/default.gif\";
}
?>

That if() statement will go just before the line:

<?php
<img src=\" echo $bg \">
?>

Which is in your header file right? So, there's no need to edit all 400+ pages. If $bg is already set, then, it won't go to default.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Nice Renegade, that works perfectly.

He has: 698 posts

Joined: Jul 2005

Right, you could place my code in the header file as well. I must've missed that you had a header file. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.