0

The error CODE below :

$ejString = (str_replace(' ', '', file_get_contents('ej.txt')));
$find_tr = strpos($ejString, 'JUMLAHRP');

function find_nilai ($ejString, $find_tr){
    while ( $find_tr != false) {
        $find_tr = strpos($find_tr, 'JUMLAHRP');
        $ejString = substr($ejString, $find_tr +8);
        echo get_string_between($ejString, $start, $end);
    }
    return $find_tr;
    return $ejString;
}
// var_dump ($find_tr);
find_nilai ($ejString, $find_tr);

i need to run this code, i dont know how to return more than one value in php

3
  • 6
    if you want to return two values, you should return an array or object which contains those two values inside it. Commented Mar 4, 2020 at 23:11
  • You should use !== false because strpos() can return 0, which is equal to false. Commented Mar 5, 2020 at 0:23
  • 1
    Welcome to stackoverflow. Your function is mutating the parameters it is passed to. Which can sometime lead to undesirable effects. It is best to make copies of the data to work on and keep the input parameters intact. Read more about that here: phptherightway.com/pages/Functional-Programming.html Commented Mar 5, 2020 at 6:10

3 Answers 3

1

A return statement terminates the execution of the function, so the return you have underneat the other will never get called. You can see more in this documentation page

To return more than one value, you could use an array or a hash, combine the values you need, and then return that variable. Or consider composing more than one function.

Sign up to request clarification or add additional context in comments.

Comments

1

You can create a wrapper class around as many data properties as you want, and then declare a new instance of the class, fill it with data, and return that instance.

Once it is received by the other end, it can be queried for its properties values.

$ejString = (str_replace(' ', '', file_get_contents('ej.txt')));
$find_tr = strpos($ejString, 'JUMLAHRP');

class myData {
    public $find_tr;
    public $ejString;
}

function find_nilai ($ejString, $find_tr){
    while ( $find_tr != false) {
        $find_tr = strpos($find_tr, 'JUMLAHRP');
        $ejString = substr($ejString, $find_tr +8);
        echo get_string_between($ejString, $start, $end);
    }
    $obj = new myData();
    $obj->find_tr = $find_tr;
    $obj->ejString = $ejString;

    return $obj;

}
// var_dump ($find_tr);
$test = find_nilai ($ejString, $find_tr);
echo $test->find_tr;
echo $test->ejString;

Comments

0

As you only need to return two elements I would suggest using an array,

$ejString = (str_replace(' ', '', file_get_contents('ej.txt')));
$find_tr = strpos($ejString, 'JUMLAHRP');

function find_nilai ($ejString, $find_tr){
    while ( $find_tr != false) {
        $find_tr = strpos($find_tr, 'JUMLAHRP');
        $ejString = substr($ejString, $find_tr +8);
        echo get_string_between($ejString, $start, $end);
    }

    return array($find_tr, $ejString);
}
$result = find_nilai ($ejString, $find_tr);
var_dump($result[0]);
var_dump($result[1]);

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.