0

I have a code for dynamic tree view using php mysql.

function fetchCategoryTreeList($parent = '', $user_tree_array = '') 
{
  // code here 
}

i just want ...like. i have a variable

$top = '1234';

now how to put in this function like

function fetchCategoryTreeList($parent = $top, $user_tree_array = '') 
{
  // code here 
}

if i put $top in this function then i got fatal error. Please help me

4
  • Can't have a fatal error just by adding $top = '1234' ; into the function. We need your real code to see the problem. Commented Jul 22, 2017 at 10:09
  • 1
    You can't set default value as another variable. Commented Jul 22, 2017 at 10:10
  • @PierreGranger He got error because of $parent = $top in function argument Commented Jul 22, 2017 at 10:11
  • Oops sorry that was obvious. Op, why you want to do that ? $parent='' in function declaration is just a "default" value, most of the time you don't have to set one. Il you really need : function foo($var,$var2) { global $top ; if ( $var == null ) $var = $top ; ... } Commented Jul 22, 2017 at 10:13

2 Answers 2

1

You cant assign default value as another variable to arguments. You can use constant instead

define("TOP", "1234");
function fetchCategoryTreeList($parent = TOP, $user_tree_array = '') 
{
  // code here 
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you really need a default dynamic value :

$top = '1234' ;

// Some code

function fetchCategoryTreeList($parent = '', $user_tree_array = '') 
{
  global $top ;
  if ( $parent == null || $parent == '' ) $parent = $top ;
  // code here 
}

But if the value is constant take a look at B Desai answer.

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.