0

I've set a few variables:

$field = "XYZ";
$block_hi = $field."_hi";
$block_lo = $field."_lo";

Then I have an object with properties that have the name of my above variables:

$obj->XYZ_hi['val'] = "value1";
$obj->XYZ_lo['val'] = "value2";

I thought I could use PHP's variable variables to reference the properties:

print( $obj->${$block_hi}['val'] );
print( $obj->${$block_lo}['val'] );

I expected to get:

value1
value2

However those lines throw errors in apache's error_log:

PHP Fatal error:  Cannot access empty property in script.php
1
  • 1
    ${$block_hi it should be $node->{$block_hi}['val'] Commented Sep 9, 2015 at 2:11

1 Answer 1

3

This would work, you had the double $$ which wasn't needed in this instance.

 $field = "XYZ";
 $block_hi = $field."_hi";
 $block_lo = $field."_lo";

 print($node->{$block_hi}['val']);
 print($node->{$block_lo}['val']);
Sign up to request clarification or add additional context in comments.

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.