vendredi 17 août 2018

Php if elseif loop is not working fine

I have below php code . It's not working. Either it just checks with if condition . If it doesn't find in If then it jumps directly to else part and print Neutral.

In my below code. I declared three arrays . One array with negative words , second with suggestion words and third array with negative words. Then i take a string and one by one checks if that string/text has negative word. If yes then print negative , otherwise check in suggestion word array. If find any suggestion word in text then print suggestion. If doesn't find suggestion word then go to positive array and search in it and so on.... Please help ASAP.

In my below code , it should print "positve" but it is printing "neutral"

<?php

$neg_words= array('not good',
'poor',
'late',
'wrong');


$sug_words=array('would',
'should',
'suggestion',
'want');

$pos_words=array('Great',
'great',
'good',
'smile',
'pleasant',
'interesting',
'pleasing',
'nice',
'happy',
'love',
'like',
'loving',
'liking',
'amazing');

$string = 'I like the way';

$tmp =explode(' ', $string);

$strings=end($tmp);




if (in_array($strings,$neg_words)):
echo "Negative"; 
elseif (in_array($strings,$sug_words)):
 echo "Suggestion"; 
elseif (in_array($strings,$pos_words)):
echo "positive";
else:
 echo "Neutral"; 

endif;



?>

Aucun commentaire:

Enregistrer un commentaire