1

I'm a .net programmer that recently decided to check PHP out and so far I must say that it's quite fun.

I use WAMPServer to work with PHP and I'm having a problem when using by reference variables.

This is the code I'm using:

function drawCategories($parent_id, $catlistids="",$level=0,$selected="") {
    global $USERLANG;
$result = mysql_query("
    SELECT 
        BPPENNYAUTOBID_categories.cat_id,
        BPPENNYAUTOBID_cats_translated.cat_name 
    FROM
        BPPENNYAUTOBID_categories 
            INNER JOIN BPPENNYAUTOBID_cats_translated ON BPPENNYAUTOBID_categories.cat_id=BPPENNYAUTOBID_cats_translated.cat_id
    WHERE
        BPPENNYAUTOBID_cats_translated.cat_name!='' 
        AND BPPENNYAUTOBID_categories.parent_id='".$parent_id."' 
        AND BPPENNYAUTOBID_cats_translated.lang='".$USERLANG."' 
    ORDER BY 
        BPPENNYAUTOBID_categories.cat_name"
);

    while ($line = mysql_fetch_array($result)) {
        if($catlistids != "") { $catlistids .= "<br />"; }
        $spaces = "";
        for($i=0;$i<$level;$i++) $spaces .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        $catlistids .= "<option value='".$line['cat_id']."' ".($selected==$line['cat_id'] ? " selected ":"").">".$spaces.$line["cat_name"]."</option>";


        drawCategories($line["cat_id"], &$catlistids,$level+1,$selected);
 }
 return $catlistids;
}

When I call the drawCategories function the second time passing the variable $catlistids by reference then all the website content disapears, I don't get any kind of error but I suppose it's something to do with WAMP server definitions.

Can anyone help me solve this problem?

Thanks in advance

5
  • 1
    How do you pass the variable by reference? Commented Sep 7, 2013 at 10:08
  • As you can see I'm calling the function drawCategories within itself and when this call is made I use the & symbol on the $catlistids variable in order to send it by reference. At least this is what I've read around the web. Commented Sep 7, 2013 at 10:13
  • is error reporting on? ini_set('display_errors', true); error_reporting(E_ALL); Maybe now you'll get an error. Commented Sep 7, 2013 at 10:27
  • @andrefrua: you say you start learning: you should not use the mysql_ extension, it has been deprecated a long time ago and is outdated (see the red box at the top). You can use either the mysqli_ extension, which will provide you with pretty much the same api, or even better use PDO which is the recommanded way to do it (it's a light database abstraction layer included in php core). Commented Sep 7, 2013 at 10:39
  • @andrefrua: you should also look into used parameterized queries rather than concatenation to avoid sql injections, see How can I prevent SQL injection in PHP? and Why shouldn't I use mysql_* functions in PHP?. Happy coding :) Commented Sep 7, 2013 at 10:40

1 Answer 1

1

There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

You can pass a variable by reference to a function so the function can modify the variable. Remove & from function call and try this way:

function drawCategories($parent_id, &$catlistids="", $level=0, $selected="") {
                                  //^
Sign up to request clarification or add additional context in comments.

3 Comments

By doing that change on the code I still have the same problem. I'm probably missing something else.
I think you could have a better way to get categories. Give me some minutes.
Nevermind my previous comment. I had also the & on the original function call. I removed it and it worked like a charm. Thanks a lot for your help hallaji

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.