if $var is in $pancake

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

if $var is in $pancake

Post by lizlazloz »

ok.

what code can i use to find this, if it is possible...

Code: Select all

<?php
$var="cheese";

$pancakerecipe="eggs,rice,cheese,corn";

if ( $var is in $pancakerecipe)
{
$boolean="true";
}
else
{
$boolean="false";
}

?>
i'm guessing that 'is in' isnt the proper code... but what is?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

sorry i have no idea what $boolean is for

the explode() function might be of use here
then try this

Code: Select all

<?php 
$var ="cheese";
$pancakerecipe = "Pancake Recipe

$pancakerecipe="eggs,rice,cheese,corn"; 

if ( $var is in $pancakerecipe) 
{ 
print "$var is in the recipe for $pancakerecipe"; 
} 
else 
{ 
print ="$var is not in the recipe for $pancakerecipe"; 
} 

?>
i havent done anything like this but the explode function i believe can do something to this effect
Last edited by malcolmboston on Thu Jan 22, 2004 8:35 am, edited 2 times in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

stristr() is the first way that springs to mind. This function does not match case, if you need a case-sensitive version you can use strstr()

Code: Select all

<?php
$var="cheese";

$pancakerecipe="eggs,rice,cheese,corn";

if (stristr($pancakerecipe, $var))
{
$boolean="true";
}
else
{
$boolean="false";
}

?>
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

personnaly, i would put in in a function, but if ya dont want a function, ya could use that code too:

Code: Select all

<?php 

$var = "Cheese"; 
$var = strtolower($var);	//to be sure to catch that var even if its in caps

$pancakerecipe= array("eggs","rice","cheese","corn"); 

if ( in_array($var, $pancakerecipe) ) &#123; 
	$boolean="true";
	print "&#123;$var&#125; was found in the pancakerecipe"; 
&#125; 
else &#123; 
	$boolean="false"; 
	print "&#123;$var&#125; was NOT found in the pancakerecipe"; 
&#125; 

?>
the is_array function is more detailled at: http://www.php.net/manual/en/function.in-array.php

later
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: if $var is in $pancake

Post by McGruff »

Code: Select all

<?php

function isRecipeItem($item, $recipe)
{    
    if (substr_count($item, $recipe) > 0)
    {
        return true; // don't quote if you want a boolean value rather than a string
    }
        else
    {
        return false;
    }
}

// in use
if(isRecipeItem($item, $recipe) == true)
{
    // do something
}
?>
Last edited by McGruff on Tue Aug 09, 2005 10:27 pm, edited 1 time in total.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i would suggest using an array if its a possibility
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

$var='cheese';
$pancakerecipe='eggs,rice,cheese,corn';
if(in_array($var, explode(',', $pancakerecipe))){
  ...TRUE...
} else {
  ...FALSE...
}
Post Reply