0

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.

1
  • "if(($string_result[0] == $value) == "n"){" you are comparing a boolean with a string here, probably not what you intended to do Commented Feb 12, 2014 at 7:13

3 Answers 3

1

It will return null as you don't stop the iteration after you find the correct one.

Move return $final_result after you find your final result like

$final_result = "pe".$string_result;
return $final_result;
Sign up to request clarification or add additional context in comments.

Comments

0

in_array will help in this situation, look following customized code for your code.

<?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,1)); //store 1st Char
    $result2 = (substr($string_result, 0,2)); //store 1st and 2nd Char
    $flag=0;
    if($result === "n") //if first char is "n"
    {
        if(in_array($result2, $second_char)) //to check whether entry is present in array or not
        {
            $final_result= "String Found";
            $flag=1;
        }
    }
    else{
        if(in_array($result, $first_char))
        {
            $final_result= "String Found";
            $flag=1;
        }
    }
    if($flag == 0)
    {
    return null;
    }

return $final_result;
}
echo awalan_pe($string_result);
?>

Comments

0

By your way, i modified:

public function awalan_pe($string_result){
        $first_char = array("m", "n", "r", "l", "w");
        $second_char = array("ny", "ng");
        $str_len = strlen($string_result);
        for($i = 0; $i <= $str_len; $i++){
            foreach($first_char as $f_char){
                if(substr($string_result,$i,$i+1) == $f_char){
                    if(substr($string_result,$i,$i+1) == "n"){
                        for($j=0; $j <= $str_len; $j++){
                            foreach($second_char as $s_char){
                                if(substr($string_result,$i,$i+2) == $s_char){
                                    return "in second array value:" .$s_char;
                                }
                            }
                        }
                    }
                }
            }
        }
        return "not in first array";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.