0

Quick question with some WordPress functions:

$options = (array)get_options('value');

Doing so allows me to get an array. But how does it work?

Thanks in advance.

2
  • 1
    type casting ? Also, WP is one of the worst source-code you could ever check ... Commented Oct 27, 2013 at 14:57
  • Seems get_options() returns an object (probably STDClass). You get an array with indices as properties of the object. Commented Oct 27, 2013 at 15:38

2 Answers 2

2

PHP is a loosely typed language and assigns types to variables depending what is assigned to it and its concept is used to change the current data type of variable from one date type to another.

 $foo = 'Hello World';
 var_dump(is_string($foo));//true 
 $bar = (array) $foo;
 echo $bar[0];// no doubt string offset also can be access using $foo[offset]
 var_dump(is_array($bar));// true
Sign up to request clarification or add additional context in comments.

3 Comments

Loosely or strongly typed language - not rellevant in this case. Java is a strong typed language, casting is, however, allowed and widely used.
well in most php automatically manages to do type juggling for example $foo = 'a string'; if ($foo) it does convert string into boolean
Thanks for all your help. Essentially, doing <code>$bar = array();</code> and then <code>$bar[0] = $foo;</code> is the same as 'type casting' right?
2

It is a type casting , PHP has really good documentation for this

Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.

<?php
    $foo = 10;   // $foo is an integer
    $bar = (boolean) $foo;   // $bar is a boolean
?>

http://php.net/manual/en/language.types.type-juggling.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.