How to create variable with for loop in php [duplicate] - php

This question already has answers here:
PHP variable variables
(6 answers)
Closed 3 years ago.
I have the following php code where by a for loop create different variables. Basically what I need is, after run the code below, it should create different variables such as $o1, $o2, $o3 etc. But it is not working based on the way I implemented. Does anyone can help me how to solve this?
for($i = 0; $i <= $totalOa; $i++){
'$o'.$i = $pieces[$i];
}

Try this...
for($i = 0; $i <= $totalOa; $i++){
${"o" . $i} = $pieces[$i];
}

Try this:
You need to use variable variables.
for($i = 0; $i <= $totalOa; $i++){
$o{$i} = $pieces[$i];
}

Related

How can I combine variable and strings together and show varable in PHP? [duplicate]

This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 1 year ago.
I need to combine and show variable but now not show variable;
I need to combine and show variable but now not show variable;
someone can help me fix it
someone can help me fix it
$ab1 = 20;
$ab2 = 30;
$x = 1;
while ($x < 3){
echo '$ab'.$x;
$x++;}
result $ab1 $ab2 / I need to show variable
You can use ${} to create dynamic variable name, so do:
echo ${'ab'.$x};
Try this it should help
$ab1 = 20;
$ab2 = 30;
$x = 1;
while ($x < 3){
$result = ${'ab'.$x};
echo $result . PHP_EOL;
$x++;
}

how to break children loops only [duplicate]

This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
I won't break a loop that is in another loop
these are my codes
for ($i = 0; $i < 20; $i++) {
// some codes
for ($i1 = 0; $i1 < 10; $i1++) {
if (a condition)
{
// i want break this loop not parent loop
}
}
// some codes
}
if I use break; parent loop will break too but I won't break only work for child loop
thanks for your answers
break only breaks out of the loop it's called in. If you want to break out of an outer control structure, you can use the optional integer argument to tell break the number of structures it should break out of (e.g., in this case, break 2 would break out of the outer loop).
I used this and its worked
for ($i = 0; $i < 20; $i++) {
// some codes
for ($i1 = 0; $i1 < 10; $i1++) {
if (a condition)
{
$i1=11;
}
}
// some codes
}

How To Solve This PHP Algorithm? [duplicate]

This question already has answers here:
Test if number is odd or even
(20 answers)
Closed 4 years ago.
I am trying to create this algorithm with PHP. I want to be able to echo out the result of this operation:
1 - 2 + 3 - 4 + 5 - 6 +...100
I want to get the result of this till I get to 100.
This how I have already started the code, however I am stuck and don't know how to proceed:
<?php
$somme = 0;
$I = 1;
while($I <= 100){
}
?>
How do I go on from this?
All answers are appreciated
It's a case of knowing, within the loop, when to add and when to subtract. A simple tracker will help us here.
$somme = 0;
$i = 1;
$op = 'sub';
while($i <= 100){
$somme = $op == 'sub' ? $somme - $i : $somme + $i;
$op = $op == 'sub' ? 'add' : 'sub';
$i++;
}
As noted in the comments, you could also decide the operation based on whether $i is even or odd.

php using for to loop words without any space between them [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
How can I achieve making words loops without any space between them using for
Here is my code:
function a($var) {
for ($i = 0; $i < ; $i++)
{
echo "a";
}
}
a(3);
I want it to make loops like:
a(3);
output: aaa
but I get an error saying
syntax error, unexpected ';'
Syntax Error
I think you are missing a limiting factor if for loop
for ($i = 0; $i < ; $i++)
should be
for ($i = 0; $i < $some_limiting_factor_here; $i++)
in your case $some_limiting_factor_here could be $var

Echo out html set number of times - php [duplicate]

This question already has answers here:
make string of N characters
(5 answers)
Closed 2 years ago.
I'm creating quite a complex html <table> layout and at this early stage it quite time consuming for me to copy and paste each <tr> in order to generate dummy content.
My idea was to specify a dummy <tr> as a $var and then output that x number of times using a function as below:
$html = "<tr>//content</tr>";
function dummy_html($html, $times){
$i = 0;
for ($i <= $times) {
echo $html;
$i = $i++;
}
}
echo dummy_html($html, 5);
But this is returning a parse error on the for line any idea why that might be ?
PHP has a function already
echo str_repeat($html,5);
Your for loop is incorrect. It should be something like:
for( $i = 0; $i <= $times; $i++ ) {
echo $html;
}
Update
#Your Common Sense's solution is better: str_repeat (http://php.net/manual/en/function.str-repeat.php)
http://php.net/manual/en/control-structures.for.php
for should use the notation: for (set arguments, conditions, command to run at the end of the loop), therefor should be:
for($i = 0; $i <= $times; $i++)
Also, I would recommend using str_repeat (http://php.net/manual/en/function.str-repeat.php)

Categories