iBrazilian2
iBrazilian2

Reputation: 2293

Why is page getting undefined variable only at first page load?

When the page loads for the first time, it also retrieves the facebook likes for that page, so I then wrote a function that saves the data to MySQL database to cache it, although on every first page load I get undefined variable: DataToSave on line 70 and 71.

This is how I am running the function.

echo $socialCounter->socialSharingCache('facebookLikes', $socialCounter->pageCounter($socialCounter->getLikes()))

The getLikes function works perfect, and retrieves the data, the issue and only issue that I am having is when I load the page and the socialSharingCache function doesn't recognize $dataToSave which is the amount of likes that page has..

public function socialSharingCache($saveName, $dataToSave)
{           
    $postDay = get_post_time( 'd', true, get_the_ID(), false );
    $dateToday = date('d');

    if( $postDay === $dateToday ) 
    {
        $setKey = 'socialSharingBar_'. get_the_ID() . '_' . $saveName;

        $getTransient   = get_transient ( $setKey );

        if( $getTransient !== FALSE)
        {
            return $getTransient;
        } else {
            if(is_wp_error($dataToSave))
            {
                return $getTransient;
            } else {
                set_transient($setKey, $DataToSave, 60 * 20 );
                update_option($setKey, $DataToSave);
                return $dataToSave;
            }
        }
    } else {
        echo 'Post was not from today';
    }   
}

Once I reload the page, the error goes away but the $dataToSave is not inserted in the set_transient because on the first pageLoad it is not passing the data, and I can't figure out how to make the function recognize it.

If I go to database and set the data to any number, it retrieves it just as it's suppose to, the only issue is passing it to the function, why isn't it?

Is it because the PHP function runs quicker than the JSON get which doesn't have the data set, so it says it's undefined?

If so, what is a way that I can overcome?

Upvotes: -1

Views: 136

Answers (1)

Jigar
Jigar

Reputation: 3322

php variables are case sensitive. $dataToSave and $DataToSave are different.

The variable name is case-sensitive.

http://php.net/manual/en/language.variables.basics.php

Upvotes: 1

Related Questions