Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:41:39+00:00 2026-05-27T02:41:39+00:00

I pass a variable $foo to a function. This input variable might be an

  • 0

I pass a variable $foo to a function. This input variable might be an array or a string. If it is an array, then I want to set:

$name = $foo['name']

And if it is a string, then I want to set:

$name = $foo

To accomplish this, I wrote the following code:

$name = isset($foo['name']) ? $foo['name'] : $foo

Unfortunately, this does not work.
The reason: isset($foo[‘name’]) returns TRUE when $foo is a string.

This behavior is suprising to me. I had expected isset($foo[‘name’]) to return FALSE because $foo[‘name’] is not set. Can someone explain this seemingly counter-intuitive behavior? Or suggest an alternative to isset() that returns FALSE on $foo[‘name’], when $foo is a string?

UPDATE:
See Christopher’s suggestion below:

$name = ( is_array( $foo ) && isset( $foo['name'] ) ) ? $foo['name'] : $foo;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:41:40+00:00Added an answer on May 27, 2026 at 2:41 am

    This shouldn’t be at all surprising.

    Why it evaluates true when $foo is a string: $foo[‘name’] is the same thing as asking for the zeroeth character of $foo when it’s a string – because ‘name’ or ‘camel’ or ‘cigar’ or ‘any other string’ will be coerced into an integral value in that context.

    array_key_exists() might also not be quite what you want, since it will return true if the key is set even if it has the null value.

    in_array() would also be bad, as it will quietly search a string or an array without complaint.

    Consider instead:

     $name = ( is_array( $foo ) && isset( $foo['name'] ) ) ? $foo['name'] : $foo;
    

    Note that this does not take into account whatever you do plan to do if array and value for key ‘name’ is null, or if string and $foo is null, etc., which isn’t a shortcoming on the part of the language but rather a piece of logic specific to your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass a variable to a UIButton action, for example NSString *string=@one;
I want to pass a variable to he buttonEvent method in the selector. [button
I am trying to pass one variable to a jQuery function inline (i.e.: using
I'm trying to pass a variable through a ssh connection, like this: working_dir=/home/user/some_dir/ ssh
Is there a method to pass a variable number of arguments to a function
I have a function 'foo' and a variable '$foo' referencing it. function foo {
I'd like to pass a value type to a function and set it to
It is possible to pass 2d array to a function as a paramter ?
Is it possible, in php, to pass variables to functions like this: function myfunction($var1=bar,
I have a templated function which I want to specialize foo to const char[N]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.