How to set a variable to a string? - php

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;

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);

Displaying All Array Contents

I have problem assigning values to the $word as array so that I can make it as a variable.
I am displaying an output of
1,000,000,000 they have all different echos; I want to make them as 1 value only example:
$output = $arramt[1]="1",$arramt[2]="000",$arramt[3]="000",$arramt[4]="000"
function mnyFmt($word){
$n = strlen($word);
if ($n%3==0){
$i = $n/3;
$x=0;
while($x<$i-1){
echo substr($word,-$n,3).",";
$arramt[$x] = substr($word,-$n,3).",";
$x++;
$n = $n-3;
}
echo substr($word,-$n,3);
$arramt[$x] = substr($word,-$n,3).",";
}
elseif($n%3==1){
$word1 = substr($word,1,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,1).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$arramt[$x] = substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
$arramt[$x] = substr($word1,-$w1,3).",";
echo array_values($arramt[$x]);
}
elseif($n%3==2){
$word1 = substr($word,2,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,2).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
}
}
$amount = 1000000000;
mnyFmt($amount);
}
Thanks very much in advance.
You don't need to make any of the custom function you can simply use number_format and explode function like as
$amount = 1000000000;
print_r(explode(',',number_format($amount)));
Demo

PHP replace echo in str_replace

I would like to use a php function for a string. Here's an example:
$txt = '123';
echo $fin = str_replace('2',"<?php echo 8; ?>",$txt);
I would like to obtain this:
183
But this is what I get:
13
Tr this
$txt = '123';
$fin = str_replace('2','8',$txt);
echo $fin;
You don't need <?php echo 8; ?>
juste do echo $fin = str_replace('2','8',$txt);
You can use a variable instead of a string if thats what you need:
$txt = '123';
$search = '2';
$replace = '8';
$end = str_replace($search,$replace,$txt);
echo $end;
This way you can use a function to change the search and change string if needed.

Naming PHP object children in for loop

I want to do this:
<?php echo $factuurgegevens->aantal.$i; ?>
But it doesn't work, how can I handle this?
So I have to get:
<?php echo $factuurgegevens->aantal1; ?>
<?php echo $factuurgegevens->aantal2; ?>
<?php echo $factuurgegevens->aantal3; ?>
<?php echo $factuurgegevens->aantal4; ?>
You can use curly braces ({}) to make up the dynamic variables:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
However you should really use an array because that is made for things like this:
$factuurgegevens->aantal = array();
$factuurgegevens->aantal[1] = 'something';
$factuurgegevens->aantal[2] = 'something';
$factuurgegevens->aantal[3] = 'something';
Do this:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
You can do this by placing your string in a variable.
for($i=0;$i<10;$i++)
{
$var = 'aantal'.$i;
echo $factuurgegevens->$var;
}

PHP a concatenate variable issue

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;
}
?>

Categories