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 there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

[PHP/WordPress] Why does this code work?

  • 26-04-2015 3: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: 798 ✭✭✭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: 6,289 ✭✭✭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