0

The function below is working fine, but not returning the value for only the variable $field in the string given to $reslt:

function req($slice,$field)
{
   if ($slice == "") {
      $reslt =  $field. ' cannot be empty<br />'; 
      return $reslt;
   }else{
      $reslt = "";
      return $reslt;
   }
}
req($slice,$field);
$err_mess = req();
echo $err_mess; // gives me 'cannot be empty' as result but does not show the value for the '$field' variable included in the string
3
  • that means your $field variable is also empty Commented Dec 22, 2015 at 16:34
  • @Surace no it isn't, i have checked, it is got value Commented Dec 22, 2015 at 16:35
  • Should there be a period (.) after $field in the fourht line? $reslt = $field. Commented Dec 22, 2015 at 16:46

1 Answer 1

1

Here:

req($slice,$field);
$err_mess = req();

you are calling your function twice: once discarding the result, the second time without parameters.

I reckon you need

$err_mess = req($slice, $field);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, adding the parameters did it, but one quick question, why do you say i am calling the function twice. Am only calling the function once, second time i am trying to get the return result into a variable
@Manu, that's not how it works. Read the paragraph on functions in the PHP manual again.
can you please tell me the best way to return the $reslt variable to $err_mess the right way ... I have already gone through the document you provided, the exixting code is the besti could come up with
Exactly how I describe in my answer.

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.