
05-26-05, 01:59 PM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
String to...
i have string like this:
$mystr = "1,2,3";
and i want to use that string like the script (bold red text) below:
<?
$okvar = 1;
if ($okvar == (1||2||3)) {
echo "OK";
} else {
echo "ERROR";
}
?>
(1||2||3) is from $mystr
How to do that?
|

05-26-05, 02:03 PM
|
|
Coding Addict
|
|
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, not quite understand you, but:
$okvar = '(' . str_replace(',', '||', $mystr) . ')';
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
|

05-26-05, 02:14 PM
|
|
Newbie Coder
|
|
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I kinda understand but at the same time i don't 
Ill just assume
|

05-26-05, 02:19 PM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
no, not like that.
+++++++++++++++++++
if $okvar must 1 or 2 or 3
+++++++++++++++++++
1 or 2 or 3 are from $mystr
|

05-26-05, 02:22 PM
|
|
Newbie Coder
|
|
Join Date: Dec 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
PHP has a function called in_array: http://us4.php.net/manual/en/function.in-array.php
So you could do in_array(1,$mystr);
It returns boolean so you could do:
If you use the third parameter it wont work unless you do '1' because the strict parameter compares the type.
Let me know if that helps.
|

05-26-05, 02:25 PM
|
 |
Newbie Coder
|
|
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This should work, if I understand your problem correctly.
|

05-26-05, 02:29 PM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
YES, Almost, but it makes Loops.
outputs:
---------------------
OK
ERROR
ERROR
---------------------
Quote:
|
Originally Posted by Jaffizzle
I kinda understand but at the same time i don't 
Ill just assume
|
|

05-26-05, 02:38 PM
|
|
Newbie Coder
|
|
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
what if he has "1,2,3,55"
and $okvar was 5?
it would pass when it should fail
wheezy's method will work!
Last edited by Jaffizzle; 05-26-05 at 02:41 PM.
Reason: oops forgot something
|

05-26-05, 02:44 PM
|
 |
Newbie Coder
|
|
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
<?
$mystr = "1,2,3";
$okvar = 1;
if(in_array($okvar, $mystr)){
echo "works";
}else{
echo "error";
}
?>
Warning: in_array() [function.in-array.html]: Wrong datatype for second argument in c:\www\test\test.php on line 4
error
|

05-26-05, 03:56 PM
|
|
Newbie Coder
|
|
Join Date: Dec 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ahhh. You'll still need need to use an explode and put it into an array. Sorry about that.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|