Getting object variable using string + variable - php

I would like to do something like this:
echo $myObject->value_$id but I don't know proper syntax and I'm not sure if it is possible.
$id is some PHP variable, for example has value 1. In the end, I would like to get $myObject->value_1 but the number part (1) should be dynamic.

The feature is called variable properties:
<?php
$myObject = (object)NULL;
$myObject->value_1 = 'I am value nr 1';
$id = 1;
echo $myObject->{"value_$id"};

This works:
$variableName = 'value_whatever_1337';
echo $myObject->$variableName;

$name = "value_" . $id;
echo $myObject->$name;

Related

PHP append json with $ to http request body

$body->append('{"stage_id": "43"}');
I would like to make the "43" dynamic and use a $variable for the value: How should look the Syntax look like?
You can use just like this:
<?php
$var = 43;
$array = array('stage_id'=>$var);
$json = json_encode($array);
$body->append($json);
?>
This will help you
$body->append('{"stage_id": $variable}');
First of all you have to declare a variable and assign your dynamic value in it as per your requirement.
$variable = Value; // if value is number like 0,1,2,3,4 or anything.
$variable = "value"; // if value is string put it inside ' or " quotes.
Now you can use your value as a variable:
body->append("{'stage_id': '$variable'}");

PHP how to echo variable name

I'm kinda confused in my case,
$counts="$s{$process[$count]}";
if i echo $counts with
echo "$counts";
it still showing the value, how can i echo $counts to show the variable name
$steamroller
(in this case the process is 'team', and count is 'roller')) instead of the value of the variable $system?
EDIT
okay i found it by myself it should be
$counts='$s'.$process."[".$count."]";
thank you..
Just make this:
$s = 's';
$count = 1;
$process[$count] = 'teamroller';
echo $counts = "$$s{$process[$count]}"; // $steamroller
if you echo variable name in "" , php parse that variable an echo its value,
if you want echo variable name with '' like this:
echo '$counts';

creating variable name by concatenating strings in php

I have a file named questions.php with an array as follows :
$question12 = array("Which is the tallest mountain","Mt Everest");
I am including this file in another file as follows :
require_once('questions.php');
$var = 12;
$question = '$question'.$var.'[0]';
echo $question;
The above code just outputs the following string (not the contents of the variable):
$question12[0]
But I want the variable $question to contain the string present in $question12[0].
How do I accomplish this?
Variable variable is not recommended, but the answer is below:
$question = ${'question'.$var}[0];
You're looking for variable variables.
$id = 12;
$q = "question{$id}";
$q = $$q[0];
You should seriously consider looking into multidimensional arrays to stop having multiple arrays.
Just use $question12[0]. It will give you the desired output.
Using the $var you can do it like this:-
$question = ${'question'. $var}[index];
Sorry, im going to get some hate for mentioning something evil but still it is one of the options
<?php
$question12 = array("Which is the tallest mountain","Mt Everest");
$var = 12;
$question = '$question'.$var.'[0]';
eval("echo $question;");
?>
P.S: eval() is that evil

Using array value with index as Variable Variable

The title may be a little confusing. This is my problem:
I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:
$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;
That would print: "hello". Now, I've got a problem with this:
$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;
That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?
Thanks in advance.
For the moment, I could only think of something like this:
<?php
// literal are simple
$literal = "Hello";
$vv = "literal";
echo $$vv . "\n";
// prints "Hello"
// for containers it's not so simple anymore
$container = array("Hello" => "World");
$vv = "container";
$reniatnoc = $$vv;
echo $reniatnoc["Hello"] . "\n";
// prints "World"
?>
The problem here is that (quoting from php: access array value on the fly):
the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.
Would PHP allow the subscript notation anywhere, one could write this more dense as
echo $$vv["Hello"]
Side note: I guess using variable variables isn't that sane to use in production.
How about this? (NOTE: variable variables are as bad as goto)
$variablename = 'array';
$key = 'index';
echo $$variablename[$key];

PHP Variable Variables

How does php handle something like this...
$blah = "Testing a variable";
$$blah = "test";
What would my new variable name be?
Everything you need to know about variable variables at http://www.php.net/manual/en/language.variables.variable.php, except for one thing: don't use them.
echo ${'Testing a variable'};
However, you don't want to do this in practice. It makes for unmaintainable, bug-prone code.
The variable $blah must contain a valid variable name.
This will tell you about variables: http://www.php.net/manual/en/language.variables.basics.php
Not really an answer, but...
<?php
function I_love_you()
{
return "haha";
}
$haha = "HoHoHo";
$tom = "I_love_you";
$blah = "tom";
echo ${$$blah()};
?>

Categories