I try to run the code below:
- 1) I check first char of $string_result
- 2) if first char of $string_result match in array first_char it will output string result
- 3) if first char equal to "n", I compare the first two char of $string_result to array second_char
4) If i remove the nested if n else, it works well. Did I do some logic errors there?
<?php $string_result = "nyanyi"; function awalan_pe($string_result){ $first_char = array("m", "n", "r", "l", "w"); $second_char = array("ny", "ng"); $result = (substr($string_result, 0,2)); foreach ($first_char as $value){ if ($string_result[0] == $value) { $final_result = "pe".$string_result; if(($string_result[0] == $value) == "n"){ foreach ($second_char as $value){ if($result == $value){ $final_result = "pe".$string_result; } } } } else{ return null; } } return $final_result; } echo awalan_pe($string_result); ?>
sadly it returns null.