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
Related
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.
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.
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
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)
This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 9 years ago.
I upgraded to PHP 5.4 today and I am receiving some strange warnings:
Warning: Illegal string offset 'quote1' in file.php on line 110
Warning: Illegal string offset 'quote1_title' in file.php on line 111
Those lines are this part of the code:
for($i = 0; $i < 3; $i++) {
$tmp_url = $meta['quote'. ($i+1)];
$tmp_title = $meta['quote' . ($i+1) .'_title'];
if(!empty($tmp_url) || !empty($tmp_title)) {
$quotes[$src_cnt] = array();
$quotes[$src_cnt]['url'] = $tmp_url;
$quotes[$src_cnt]['title'] = $tmp_title;
$src_cnt++;
}
}
So the $tmp_url and $tmp_title line.
Why am I receiving this odd warning and what is the solution?
Update:
This code is being used as a Wordpress plugin. $meta includes:
$meta = get_post_meta($post->ID,'_quote_source',TRUE);
So I am suspecting that whenever the quotes fields are empty, this warning appears. Is there any way that I can fix this for when the fields are empty?
You need to make sure, that $meta is actually of type array. The warning explicitly tells you, that $meta seems to be a stringĀ and not an array
Illegal string offset
^^^^^^
To avoid this error you may also check for the needed fields
for($i = 0; $i < 3; $i++) {
if ( !is_array($meta) || !array_key_exists('quote'. ($i+1), $meta) ){
continue;
}
// your code
}
If $meta is null whenever there's no data to process:
if( !is_null($meta) ){
for($i = 0; $i < 3; $i++) {
// ...
}
}
You should be able to do more checks if necessary. That depends on what that get_post_meta() function is designed to return.