Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

[PHP/WordPress] Why does this code work?

  • 26-04-2015 4:54pm
    #1
    Closed Accounts Posts: 4,763 ✭✭✭


    For my current client I need to get the ID of the ultimate parent of a category. The Bestest Way to do this is to iteratively call get_category() on successive parents until you hit the top category, but being bored and having a pint in me I decided to use a recursive function.

    This function works-it correctly returns the top category ID-but logic tells me that the function should be returning an error, null or 0. This is a quirk of either WordPress or PHP, and I would like to better understand why this function works, because I feel this will teach me a fundamental lesson about PHP:

    https://gist.github.com/bhalash/c6c8e9eed7bdc4c87cb8
    function get_category_parent_id($cat_id) {
        $category = get_category($cat_id);
        $parent = $category->category_parent;
    
        if ($parent) {
            get_category_parent_id($parent);
        } 
    
        return $parent;
    }
    

    What I expect to happen is that I will eventually pass null as a parameter to get_category, which will return an error, so when I look at the output of this function I expect to see either null or an error object.

    I say this because I assign the value to the parent before I use it, so I eventually assign an error object or null to the value and then return it.


Comments

  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    Fenster wrote: »
    What I expect to happen is that I will eventually pass null as a parameter to get_category

    Why would you expect that? It explicitly checks that the $parent is not null, so it never passes that to the function.


  • Registered Users Posts: 793 ✭✭✭FobleAsNuck


    if $parent==null then this block will not run


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    1. I fetch a category based on $cat_id.
    2. I look at the object, get category_parent and assign it to $parent.

    When a category has no parent, category_parent returns false.
    I have now assigned a value of false to $parent, skip the loop and return false.

    Either way, I expect a return value from the overall function of null, false or error.

    Better explanation:

    Grandchild ID: 192
    Child ID: 137
    Parent ID: 101

    Level #0:
    $cat_id = 192
    $parent = 137

    $parent is true, so go to level #1.

    Level #1:
    $cat_id = 137
    $parent = 101

    $parent is true, so go to level #2.

    Level #2:
    $cat_id = 101
    $parent = 0/false

    $parent is false, but has a value of false, so return $parent, with a value of false.

    Again, I coded this pretty ****tily and I don't understand why it returns, say, the parent ID of 101 instead of false/null/error.


  • Registered Users Posts: 5,995 ✭✭✭Talisman


    Did you look at the get_category_parents function?


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    Yeah. I'm still not sure why the original worked, but here's the non-drunk version of the function:
    function find_category_parent_id($cat_id = null) {
        $category = get_category($cat_id); 
    
        if ($category->category_parent) {
            $cat_id = find_category_parent_id($category->category_parent);
        }
    
        return $cat_id;
    }
    

    Thank you all for your answers!


  • Advertisement
Advertisement