4

I have this simple one line code:

$thisUserName = is_array($user) ? $user->name : $user;

Here, $thisUserName is giving by $user, means, the condition is_array is returning false, even if print_r is showing that $user is an array.

Any idea, anybody ?

Thanks.

PS. I tried changing that to echo is_array($user) ? 'yes' : 'no' and it is echoing no.

EDIT:

print_r($user) gives

stdClass Object
(
    [id] => 169
    [name] => Cedric
    [username] => pulpfiction
    [email] => [email protected]
    [password] => c22601b4ed1ac11a80955d6c0eeb1933
    [password_clear] => 
    [usertype] => Registered
    [block] => 0
    [sendEmail] => 0
    [gid] => 18
    [registerDate] => 2013-01-30 11:12:10
    [lastvisitDate] => 2013-02-24 19:45:45
    [activation] => 
    [params] => 

    [aid] => 1
    [guest] => 0
)
4
  • 2
    Is $user an array or an instantiated object? Because $user->name should error is $user is an array Commented Mar 1, 2013 at 12:04
  • 2
    You do realize that the -> operator belongs to objects, not arrays, right? Maybe you need is_object() instead? Or maybe you need to use []` instead of ->. Commented Mar 1, 2013 at 12:05
  • var_dump($user) shows what? Commented Mar 1, 2013 at 12:05
  • if you want to check that user in array then maybe it should be in_array. Commented Mar 1, 2013 at 12:07

3 Answers 3

15

$user is an object not an array. print_r() will still output it all nice looking, but if you look closely at the output you will see not everything will say array, it will have have the object listed. You should be using is_object()

$thisUserName = is_object($user) ? $user->name : $user;
Sign up to request clarification or add additional context in comments.

6 Comments

Jackpot Cryptic. How did you guess that $user would be an object here ?
@Jeremy - the -> syntax is for objects. Arrays use =>
@Jeremy $user->name is the property name of the object $user
I would prefer to use instanceof to confirm the actual class of the object, rather than just is_object().
@Jeremy if you can post the output of print_r @SDC can provide you with a working example using instanOf but we need to know the name of the class.
|
2

is_array() checks whether the variable is an array.

However, your code immediately after it is $user->name, which implies that you are actually expecting it to be an object. is_array() will not return true for objects.

If you want to test if it's an object, you could use is_object() instead of is_array().

However, it would be even better to check if it's an object of the type you expect, in which case you could use instanceof eg:

$thisUserName = ($user instanceof myUserClass) ? $user->name : $user

(where 'myUserClass' is the class name of your user class, obviously)

Hope that helps.

[EDIT]

In a good system, one should know the class name that is expected for any given object, and use instanceof to verify it.

But if you don't know the class name to expect then is_object() will suffice.

(You can find out the class name of an object using get_class(); that might be worthwhile for your own purposes to learn about the system you're using (Drupal?), but there's little point using get_class and intanceof together in this context to actually test the object; just stick with is_object())

1 Comment

edited answer to answer @Jeremy's confusion about not knowing the class name.
0

If you are using object ,Then try this example

    <?php
    $user->name='my_name';
    echo (is_object($user)) ?$user->name:$user;
    ?>

//Result : my_name

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.