0

I'm trying to reuse a function, but it's not getting the arguments values:

function has_children($arg1, $arg2){
    echo $arg1.'<br>'.$arg2;
    $children = get_term_children( $arg1, 'area' );
    if(empty($children)){
        $term_children_slug = $arg2;
    }
}

One of the places where it is used

if (is_tax('area')){
    $term_slug = $queried_object->slug;
    $term_id = $queried_object->term_id;
    has_children($term_id, $term_slug);
}

The values are printed but not used inside the function.

11
  • If you try echo $children is there anything displayed? Commented Feb 9, 2015 at 14:06
  • Do you see that echo? echo $arg1.'<br>'.$arg2;? Commented Feb 9, 2015 at 14:06
  • 1
    What is $term_children_slug? It looks like a global, but you don't declare it as such in your function... Commented Feb 9, 2015 at 14:07
  • @VeeeneX Yes, I see the values that I need to use. Commented Feb 9, 2015 at 14:07
  • 1
    @VeeeneX You mean I can't use $term_children_slug outside has_children function? Commented Feb 9, 2015 at 14:16

3 Answers 3

2

You can't access $term_children_slug, when it isn't declared as global in function scope.

function has_children($arg1, $arg2){
    /* Return type 1 */
    $term_children_slug = '';

    echo $arg1.'<br>'.$arg2; // args printed here
    $children = get_term_children( $arg1, 'area' ); // not used here?
    if(empty($children)){
        $term_children_slug = $arg2;
    }
    /* Return type 2-Remove Comments and return type 1
    return (isset($term_children_slug)) ? $term_children_slug : '';
    */

    /* Return type 1 */
    return $term_children_slug;
}
if (is_tax('area')){
    $term_slug = $queried_object->slug;
    $term_id = $queried_object->term_id;
    /* If you want to get result */
    $result = has_children($term_id, $term_slug);
}

For more details check didierc answer

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

3 Comments

Note that you only return something if $children is empty.
@didierc What about now, restful :D
Well, that's good enough. If you want an advice, I would avoid returning values of different types (although it's common practice in PHP), because it makes it harder to reason upon, if for instance you need to pass the function around. That being said, I don't know the type of values expected by Op.
0

You mean:

function has_children($arg1, $arg2){
    echo $arg1.'<br>'.$arg2; // args printed here
    $children = get_term_children( $arg1, 'area' ); // not used here?
    if(empty($children)){
        $term_children_slug = $arg2;
    }
}

How do you know they are not used in the function? What do you expect to happen?

2 Comments

I expect the function define the correct value to $term_children_slug variable, based on the condition.
$term_children_slug is not in your scope. Either make it global in has_children() or make has_children() return a value and assign it to $term_children_slug.
0

Issue

As it stands, your function has no visible side effects: it calls a function whose purpose is unknown, and modify a variable which is not declared global or returned by the function.

Possible fixes

Import the global in the function scope:

function has_children($arg1, $arg2) {
  global $term_children_slug;
  echo $arg1.'<br>'.$arg2;
  $children = get_term_children( $arg1, 'area' );
  if(empty($children)){
    $term_children_slug = $arg2;
  }
}

Return the computed value:

function has_children($arg1, $arg2) {
  $term_children_slug = 'default values';
  echo $arg1.'<br>'.$arg2;
  $children = get_term_children( $arg1, 'area' );
  if(empty($children)){
    $term_children_slug = $arg2;
  }
  return $term_children_slug;
}

Don't forget to initialise the variable with a default value (could be a value from $children).

1 Comment

True, but that's another problem. Not knowing the remaining of OP's code, I cannot know which fix will more likely fit his codebase.

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.