slashpine Posted December 14, 2008 Share Posted December 14, 2008 Hi, can someone please show me how to use an array rather than a single word string? if ($query=='painters') echo "hellow world";[/php how can I get the echo results if the query is any of the following..."painters","pressure_washing", "property_maintenance", "contractors". ? Thanks in advance... Link to comment https://forums.phpfreaks.com/topic/136962-solved-request-help-with-if-statement/ Share on other sites More sharing options...
Mad Mick Posted December 14, 2008 Share Posted December 14, 2008 A bit confused but is this what you want? $things=array("painters","pressure_washing", "property_maintenance", "contractors"); $query="painters"; //for example... foreach($things as $element){ if ($element==$query) echo $query; } Link to comment https://forums.phpfreaks.com/topic/136962-solved-request-help-with-if-statement/#findComment-715338 Share on other sites More sharing options...
slashpine Posted December 15, 2008 Author Share Posted December 15, 2008 Thanks...that works great... Now what if I wanted to add a second array that would echo a different message? Link to comment https://forums.phpfreaks.com/topic/136962-solved-request-help-with-if-statement/#findComment-715451 Share on other sites More sharing options...
Mad Mick Posted December 15, 2008 Share Posted December 15, 2008 You can use the PHP function in_array(): http://uk3.php.net/function.in-array Link to comment https://forums.phpfreaks.com/topic/136962-solved-request-help-with-if-statement/#findComment-715477 Share on other sites More sharing options...
slashpine Posted December 15, 2008 Author Share Posted December 15, 2008 use it in place of "foreach($things as $element){ if ($element==$query) echo $query; or with it ? Link to comment https://forums.phpfreaks.com/topic/136962-solved-request-help-with-if-statement/#findComment-715480 Share on other sites More sharing options...
Mad Mick Posted December 15, 2008 Share Posted December 15, 2008 In place of - my first answer was too longwinded! Note this checks every array in case $query is in more than one. $things=array("painters","pressure_washing", "property_maintenance", "contractors"); $otherthings=array("builders","cleaning", "something", "something_else"); $query="painters"; if (in_array($query,$things)) echo "in things array"; if (in_array($query,$otherthings)) echo "in otherthings array"; Link to comment https://forums.phpfreaks.com/topic/136962-solved-request-help-with-if-statement/#findComment-715499 Share on other sites More sharing options...
slashpine Posted December 15, 2008 Author Share Posted December 15, 2008 Man! that is so great! Thanks a million... Link to comment https://forums.phpfreaks.com/topic/136962-solved-request-help-with-if-statement/#findComment-715509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.