• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Comparing part of a variable value

I have a session whose value is either something like this rose_portraitlg.jpg or this rose_landslg.jpg. The only thing that is constant in these file names is the portrait or lands. I need to be able to compare variable in an if statement to determine if portrait or lands is in the variable named.

$virtualcomp = $_SESSION['virtualimage'];
...

if ( $virtualcomp == portrait) {
echo "<img src='awards/".$_SESSION['virtualimage']."' width='445' height='607'/>";
}

else {
echo "<img src='awards/".$_SESSION['virtualimage']."' width='560' height='430'/>";
}
0
pixelscape
Asked:
pixelscape
1 Solution
 
Greg AlexanderLead DeveloperCommented:
Try this:
<?
if ( preg_match("/portrait/",$virtualcomp)) {
	echo "<img src='awards/".$_SESSION['virtualimage']."' width='445' height='607'/>";
}

else {
	echo "<img src='awards/".$_SESSION['virtualimage']."' width='560' height='430'/>";
}
?>

Open in new window

0
 
Rik-LeggerCommented:
if (strpos($virtualcomp, 'portrait') !== false) {
echo "<img src='awards/".$_SESSION['virtualimage']."' width='445' height='607'/>";
} else {
echo "<img src='awards/".$_SESSION['virtualimage']."' width='560' height='430'/>";
}

Open in new window

0
 
teedo757Commented:
In case you were wondering why if (strpos($virtualcomp, 'portrait') !== false) works

The strpos() function returns the position of the first occurrence of a string inside another string.

If the string is not found, this function returns FALSE.
0
 
pixelscapeAuthor Commented:
Gotcha, thanks for the input guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now