Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

[PHP/WordPress] Why does this code work?

  • 26-04-2015 04: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, Registered Users 2 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, Registered Users 2 Posts: 801 ✭✭✭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, Registered Users 2 Posts: 7,150 ✭✭✭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