thelolcat
thelolcat

Reputation: 11625

variable key name

Can I check for a variable key without using a temporary variable.

$var = 'blabla';
$key = "{$var}_abc";

if(isset($someobject->$key))...

?

with arrays you can do this... $array["{$var}_abc"]

Upvotes: 1

Views: 1986

Answers (5)

Starx
Starx

Reputation: 79031

You can do this, using property_exists() method

if(property_exists($object, $var."_abc")) {
 // do stuff
}

Upvotes: 1

Vit Kos
Vit Kos

Reputation: 5755

you can use concatenation like $array[$var."_abc"]

Upvotes: 2

salathe
salathe

Reputation: 51960

Yes. You can use curly braces containing an expression resulting in a string, where that string is the name of the property you want to check.

$someobject->{"{$var}_abc"}
$someobject->{$var."_abc"}

Upvotes: 2

webbiedave
webbiedave

Reputation: 48887

You can employ braces around the member name:

if (isset($someobject->{$var.'_abc'}))

Upvotes: 1

Stewie
Stewie

Reputation: 3121

yes, try enclosing the variable in braces

Edit: not paranthesis, braces..

Upvotes: 1

Related Questions