Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Object Variable variables name?

Tags:

I have a loop that goes from 1 to 10 and prints values in

$entity_object->field_question_1 through 10 so...

$entity_object->field_question_1, $entity_object->field_question_2, etc

And I want to print this in this loop, how can I get the variable? I tried doing the

$var = "entity_object->field_question_".$i; print $$var; 

But that did not work...

How can I get these values?

like image 388
Steven Avatar asked Jun 06 '12 17:06

Steven


People also ask

How do you name a variable in PHP?

All variables in PHP start with a $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character _ . A variable name cannot start with a number. A variable name in PHP can only contain alpha-numeric characters and underscores ( A-z , 0-9 , and _ ).

Which 3 are correct variable names in PHP?

A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive ( $age and $AGE are two different variables)

What is variable variable in PHP?

Description. Variable is a symbol or name that stands for a value. Variables are used for storing values such as numeric values, characters, character strings, or memory addresses so that they can be used in any part of the program. Declaring PHP variables.

What is $_ in PHP?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html> <body>


2 Answers

This should work:

$var="field_question_$i"; $entity_object->$var; 
like image 89
Tim Withers Avatar answered Dec 13 '22 13:12

Tim Withers


Actually you need to take the variable outside the string like this in order to those solutions to work: $var="field_question_".$i;

$entity_object->$var;

Or

$entity_object->{"field_question_".$i} 
like image 33
user1830966 Avatar answered Dec 13 '22 12:12

user1830966