PHP a concatenate variable issue - php

I'm trying to concatenate several variables following http://php.net/manual/en/language.variables.variable.php but I don't get why it's not working
<?PHP
$test1 = 'test1';
$test2 = 'test2';
$test0 = 'test0';
for($i=1;$i<=9;$i++){
$j = $i%3;
echo ${$test.$j};
}
?>

If at all possible it would be much easier to just set the variables up as an array in the first place.
But to do what you are trying do this
$test1 = 'test1';
$test2 = 'test2';
$test0 = 'test0';
for($i=1;$i<=9;$i++){
$j = $i%3;
echo ${"test".$j};
}

try it.
$test1 = 'test1';
$test2 = 'test2';
$test0 = 'test0';
for($i=1;$i<=9;$i++){
$j = $i%3;
echo $colonne.$j;
}
?>

Related

how to call variable in php array

<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
here I am trying a random url with the variable $user and $mail with time intervals per 30 minutes.
<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
output "site.com/$user" or "site.com/$mail"
from the results of the above output I try to randomly use a loop by calling the variable $y in array $arr = array($y); but the results that come out in the loop "site.com/$user" not "site.com/username"
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
I'm breaking this into multiple steps here. You might be able to condense it:
$x = []
$userElement = 'site.com/'.$user;
$mailElement = 'site.com/'.$mail;
array_push($x, $userElement);
array_push($x, $mailElement);
var_dump($x);

PHP Loop Dynamic Variable

I am trying to create a dynamic variable. I have a loop and I want it to loop through the records and create a variable for each record. My code:
$ct = 1;
foreach ($record as $rec){
$var.$ct = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
When I try to use the above code, it gives me an error saying the $var1 variable doesn't exist/undefined? Is it possible in PHP to create dynamic variables like the above example. If so, what am I doing wrong?
You're looking for variable variables.
Create the variable name as a string, and then assign it:
$ct = 1;
foreach( $record as $rec )
{
$name = 'var'.$ct;
$$name = $rec['Name'];
$ct++;
}
echo $var1;
It would be much better to create an array, though:
$names = [ ];
foreach( $record as $rec )
{
$names[] = $rec['Name'];
}
echo $names[0];
You can use different syntax with {}
$ct = 1;
foreach ($record as $rec){
${'var' . $ct++} = $rec['Name'];
}
echo $var1;
Although isn't it better just to use an array?
Working fiddle
You can with a double $.
$var = "variable";
$$var = "test";
echo $variable;
//echoes "test"
in your example:
$ct = 1;
foreach ($record as $rec){
$varname = "var" . $ct;
$$varname = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
Please try this, let me know if it works for you.
I use a prefix with the dynamic variable.
$ct = 1;
$prefix = 'var';
foreach ($record as $key=>$rec){
$temp = $ct;
$ct = $prefix.$ct;
$$ct = $rec;
$ct = $temp + 1;
}
echo $var1;
You can do that using array easily. But if you really want it to be in dyanamic vairable individually, in that case also , I would like to suggest you to get help in array way. This way, you can track you variables.
In the below mentioned way, you event don't need to take a extra variable like $ct. Just an array $var and applying extract method on it after the loop will do the trick.
$var = [];
foreach( $record as $k => $rec )
{
$var['var'.$k] = $rec['Name'];
}
extract($var);
echo $var0; //or $var_1 or $var_2

What's opposite of eval function?

I wonder how I can use the exact opposite of the eval function.
This is my code:
$test = 1;
$t = '$test';
echo opposite_eval($t);
I have to 1 output from above codes, how can i use method, function or class ?
Thanks for your estemeed help friends !
i cheated a little by removing the $ from the $t string (you can do that in the function its just a string:
$t = 'test';
function opposite_eval($t){
$test = 1;
return($$t);
}
echo opposite_eval($t); //=1
the phase you want to look in to is variable variables
I think you want a variables variable
In your case it would be:
$test = 1;
$t = 'test';
echo $$t;
// output: 1
Addon:
You could also do things like that:
$test['x'] = 1;
$t = 'test';
echo $$t['x'];
Whereas this will not work:
$test['x'] = 1;
$t = "test['x']";
echo $$t;
// Produces: NOTICE Undefined variable: test['x'] on line number 6
neither will:
$test = new stdClass();
$test->x = 1;
$t = "test->x";
echo $$t;
but this will work:
$test = new stdClass();
$test->x = 1;
$t = "{$test->x}";
echo $t;
and this will also work:
$test =[];
$test['x'] = 1;
$t = "{$test['x']}";
echo $t;

How to set a variable to a string?

There is any possibility making something like that using PHP?
<?php
$number = 1;
$str_$number = "BlahBlah";
// and make this: $str_1 = "BlahBlah";
echo $str_1;
?>
<?php
$number = 1;
${'str_'.$number} = 'foobar';
echo $str_1;//foobar
<?php
$number = 1;
$value = 'str_' . $number;
$$value = 'blahblah';
echo $str_1;
?>
Try
<?php
$number = 1;
${'str_' . $number} = 'foobar';
?>
You can also try
$number = 1;
eval('$str_'.$number.' = "foobar";');
echo $str_1;

php - use variable variable in while loop

I'm a bit confused about variable variables.
What I like to do is print the value of $var1, $var2 and $var3:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo $var.$i;
$i++;
}
I know $var.$i; is not correct, but I think it shows what I would like to achieve; the while-loop should change it to $var1, $var2 and $var3;
I've tried the following:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
$current_var = 'var'.$i;
$current_var = $$current_var;
echo $current_var;
$i++;
}
But that doesn't work. I think because $var1, $var2 and $var3 are recreated in the while-loop instead of using the actual value. Not sure if that's correct, but that the only thing I can think of.
Try this instead:
echo ${"var".$i};
Curly braces can resolve to variable names without having to use the dollar-dollar approach.
See: Variable Variables in PHP
Try this one.
<?php
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo ${'var'.$i};
$i++;
}
?>
Are you trying to do something like this. Then use array
$my_data = array();
$my_data[1] = 'a';
$my_data[2] = 'b';
$my_data[3] = 'c';
// Method 1
$i = 1;
while ($i <= 3) {
echo $my_data[$i];
$i++;
}
// Method 2
foreach ($my_data as $data) {
echo $data;
}
// Output
abc
abc

Categories