Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Matching text does not work
Old 10-06-2009, 06:43 PM Matching text does not work
Average Talker

Posts: 21
Trades: 0
My problem can be boiled down to a snippet to see if text in $foo matches any of 3 other text items.

Here 'tis
Code:
<?
$foo = "a";

/* I want to find out if $foo matches any of 3 characters */

if ($foo == "x" || "y" || "z")
{
echo "if this prints something must be wrong";
}
echo "<br>end test";
?>
Thanks in advance for help on this.
jimandy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-06-2009, 07:58 PM Re: Matching text does not work
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Each of the conditions in an if statement is independent of one another:
PHP Code:
if($foo == 'x' || $foo == 'y' || $foo == 'z')
{
     echo 
"if this prints something must be wrong";

I'm guessing in your code the message is always displayed, since in PHP, anything non-zero is true.

If you have a lot of conditions it may be easier to use in_array():

PHP Code:
if(in_array($foo, array('x''y''z')))
{
     echo 
"if this prints something must be wrong";

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 10-07-2009 at 01:51 AM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-07-2009, 01:19 AM Re: Matching text does not work
Average Talker

Posts: 21
Trades: 0
Thanks NullPointer. Your "in_array" suggestion works and solved my problem.
But your explanation, citing this code as the correct way to use the "if" statement with multiple terms still did not work for me.
Code:
<?
$foo = 'a';
if($foo == 'x' || $foo == 'y' || $foo = 'z') 
{ 
     echo "if this prints something must be wrong"; 
}
?>
jimandy is offline
Reply With Quote
View Public Profile
 
Old 10-07-2009, 01:49 AM Re: Matching text does not work
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Typo on my part. The last condition should be:
$foo == 'z'

Within the context of an conditional, an assignment returns the value being assigned. So $foo = 'z' while technically legal, is the same as 'z'. Had this been java the complier would have pointed this out.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-07-2009, 02:06 PM Re: Matching text does not work
Average Talker

Posts: 21
Trades: 0
Thanks again, NullPointer. I didn't notice the single "=" when I tested your suggestion. Now it works, of course.

Thought I was loosing my mind. Some say I already have
jimandy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Matching text does not work
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17568 seconds with 12 queries