[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • 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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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