0

I saw this piece of code that have part of the code I never saw before. What is the behavior of (array) in this scenario? so far I know the way of creating arrays in php never see this.

//

    $this->settings = array_merge( $this->settings_defaults(), (array) get_option( $this->settings_slug, $this->settings_defaults() ) );

   //  
3

You are looking at type casting: http://php.net/manual/en/language.types.type-juggling.php

What the code does is caste the value returned by get_option() to an array. It is being done so that array_merge() works correctly and doesn't trigger warnings/errors. That much is pure PHP and is off-topic. The only reason I chose to answer rather than to post a comment is because the return value of get_option() will return various types of data depending on context. You can see that noted in the source:

@param bool|mixed $pre_option Value to return instead of the option value.

https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/option.php#L42

That means that you have to manipulate the return data in order to be sure of the type of data that you are dealing with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.