Is nested loop in PHP possible? [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
$tablecode1 = "<table>"; $column = 3; $rows = 2;
for ($m=1; $m<=$column; $m++){
$tablecode1.="<tr>"
for ($n=1; $n<=$rows; $n++){
$tablecode1.="<td>$n-$m</td>";
}
$tablecode1.="</tr>";
}
$tablecode1.="</table>";
echo "$tablecode1";
Parse error: syntax error, unexpected 'for' (T_FOR) in line # second for
The first $tablecode1 should have value of "<table>". idk why it didnt show up.

I see the missing semicolon (";") on the second line.
for ($m=1; $m<=$column; $m++){
$tablecode1.="<tr>" ; // <<== You are missing a semicolon.

Related

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

File not run in search.php [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ')' in
/wp-content/themes/dream/search.php on line 75
if (!empty($available['from'] && $available['to']) && ($theDate > $availableFrom) && ($theDate < $availableTo)) { ?>
Try this :
if (!empty($available['from']) && !empty($available['to']) && ($theDate > $availableFrom) && ($theDate < $availableTo)) { ?>

syntax error, unexpected '$type' (T_VARIABLE), expecting '(' in [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
if($_POST) {
$type = $_POST['roll'];
if $type == 1 {
$number = rand(1,100)
if $number <= 50 {
echo "Winner!"
}
else {
echo "Loser!"
}
}
};
Can't figure out what is wrong. I am kinda new to PHP also. Type (or roll) is always 1 (for now)
I think you just need to put
if ($type == 1) {
instead of your actual statement, the condition of the if need that.
And so you'll need it too on the second "if"
Watch what your error is showing, it's exactly what you need.

Parse error: syntax error, unexpected 'for' (T_FOR) in C:\wamp\www\php test 11F\test.php on line 3 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
The variable $n is readed from a form:
<?php
$nume=$_POST['n']
for($i=1;$i<=n;$i++)
{
for($j=1;$j<=n;$j++)
{
if($i==$j) $a[$i][$j]=0;
else $a[$i][$j]=$i;
echo $a[$i][$j]." ";
}
echo $a[$i][$j];
}
?>
Parse error: syntax error, unexpected 'for' (T_FOR) in C:\wamp\www\php
test 11F\test.php on line 3
What's the error, the problem?
It is my first project in php.
<?php
$n = 20;
for($i=1;$i<=$n;$i++)
{
for($j=1;$j<=$n;$j++)
{
if($i==$j)
$a[$i][$j]=0;
else
$a[$i][$j]=$i;
echo $a[$i][$j]." ";
}
}
?>
I fixed these issues,
n should be - $n
You are trying access value of $a[$i][$j] form outside the second loop - Then $j value equal to $n+1 - but you can get only 1 to $n
Outside the two loops print the array this way then you can see what are the accessible keys
echo "<pre>";
print_r($a);
echo "</pre>";
there is no semicolon after $_POST['n']. check below updated code
<?php
$nume=$_POST['n'];
for($i=1;$i<=$nume;$i++)
{
for($j=1;$j<=$nume;$j++)
{
if($i==$j) $a[$i][$j]=0;
else $a[$i][$j]=$i;
echo $a[$i][$j]." ";
}
echo $a[$i][$j];
}
?>

PHP for-loop not working [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have this for-loop in PHP which isn't working:
for ($i = 0; $i <= 100; $i = $i + 10) {
echo $i;
}
and this one that does work:
for ($i = 0; $i <= 100; $i = $i + 10) {
echo $i;
}
I'm sure that I'm missing something, but they appear to be exactly identical to each other. What's the difference? The first one was copied and pasted, the second one typed by myself. What's the difference? Why isn't the second one working? The error is this:
This is the error message:
Parse error: syntax error, unexpected ')', expecting ';' on line 12
Line 12 is the first line of the code in the sample (it's from a larger script).
The loops are exactly the same and they both work, you must have any other error on the script, although, you can use :
$i+=10;
instead of:
$i = $i + 10

Categories