0

I have variable: $default['text']="This is default text"; and $other_text['text']="This is other text";

I would like to choose one of them in function:

function insertText($param){
  if(isset($other_text[$param]))
    echo($other_text[$param]); (*)
  else 
    echo($default[$param]); (**)
}

if instead of lines (*) and (**) I write something like: echo("other_text"); and echo("default_text"); I always receive second option. That's why I assume there is something wrong with $var[$param] construction. How should that look like?

1 Answer 1

1

If $default['text']="This is default text"; and $other_text['text']="This is other text"; are defined outside the function body, then you should declare them as global inside your function:

function insertText($param){
  global $default, $other_text;
  if(isset($other_text[$param]))
    echo($other_text[$param]); (*)
  else 
    echo($default[$param]); (**)
}

If you don't do that, then the check isset($other_text[$param]) will always return false.

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

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.