We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Comparing part of a variable value

pixelscape
pixelscape asked
on
Medium Priority
230 Views
Last Modified: 2012-05-11
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'/>";
}
Comment
Watch Question

Lead Developer
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Top Expert 2011

Commented:
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

Commented:
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.

Author

Commented:
Gotcha, thanks for the input guys.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.