pneudralics Posted April 27, 2009 Share Posted April 27, 2009 Want to compare name from form to badword list, but can't seem to get it to compare. function badword() { array("badword1","badword2","badword3") } $badword = badword(); //$name comes from form $name = $_POST['name']; if ("$name" != "$badword") { //if not badword do this } else { //bad word } Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/ Share on other sites More sharing options...
kenrbnsn Posted April 27, 2009 Share Posted April 27, 2009 Use in_array <?php $badwords = array("badword1","badword2","badword3"); if (!in_array($_POST['name'],$badwords)) { // // not a bad word // } else { // // bad word // } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/#findComment-820748 Share on other sites More sharing options...
pneudralics Posted April 27, 2009 Author Share Posted April 27, 2009 I seem to get: Wrong datatype for second argument When I try your example. I notice when I try to echo the below I don't get any output except Array. $badwords = array("badword1","badword2","badword3"); echo "$badwords"; Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/#findComment-820754 Share on other sites More sharing options...
pneudralics Posted April 27, 2009 Author Share Posted April 27, 2009 Also tried the below and got Invalid argument supplied for foreach() $badwords = badword(); foreach($badwords as $value) { echo "$value"; } Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/#findComment-820757 Share on other sites More sharing options...
DarkWater Posted April 27, 2009 Share Posted April 27, 2009 That's because your badwords() function doesn't actually do anything in its current state. Plus you're missing a ;. Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/#findComment-820760 Share on other sites More sharing options...
pneudralics Posted April 27, 2009 Author Share Posted April 27, 2009 That's because your badwords() function doesn't actually do anything in its current state. Plus you're missing a ;. Shouldn't the foreach display the array? Where does the ; go? Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/#findComment-820764 Share on other sites More sharing options...
Philip Posted April 27, 2009 Share Posted April 27, 2009 function badword() { array("badword1","badword2","badword3") } Should be the following for it to work: function badword() { return array("badword1","badword2","badword3"); } As a whole: <?php function badword() { return array("badword1","badword2","badword3"); } $badwords = badword(); if (!in_array($_POST['name'],$badwords)) { // // not a bad word // } else { // // bad word // } Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/#findComment-820765 Share on other sites More sharing options...
pneudralics Posted April 27, 2009 Author Share Posted April 27, 2009 Thanks the return() fixed the issue Quote Link to comment https://forums.phpfreaks.com/topic/155914-solved-how-do-i-compare-a-variable-to-a-function-array/#findComment-820768 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.