471,596 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

"<td><input name='".$i-2."'

aa
I am traing to output the following HTML string from PHP:
"<td><input name='".$i-2."' size='2' type='text'
onBlur='myfunction(".$i-2.")'></td>"
But it somehow mistreats $i-2. PHP does not throw any errors but the
browser renders the table incorrectly.
I got round this by introducing another variable
j=i-2 and using it in place of i-2
It works. However I am interested to understand why is does not work with
i-1. Any comments?
Jul 17 '05 #1
3 2301
.oO(aa)
I am traing to output the following HTML string from PHP:
"<td><input name='".$i-2."' size='2' type='text'
onBlur='myfunction(".$i-2.")'></td>"
But it somehow mistreats $i-2. PHP does not throw any errors but the
browser renders the table incorrectly.
I get a parse error, but even without that the above would not do what
you want. In fact what you are trying do to is to print a difference of
these three strings:

"<td><input name='".$i
2."' size='2' type='text' onBlur='myfunction(".$i
2.")'></td>"
I got round this by introducing another variable
j=i-2 and using it in place of i-2
It works.


There are some other ways to solve that issue:

1) Use parentheses around the $i-2, e.g.

print "<td><input name='".($i-2)."' size='2' ...";

2) Use printf() and a format string with placeholders, which are then
replaced with $i-2. See the manual on sprintf() for details.

Micha
Jul 17 '05 #2
aa
Thanks, Micha
It never occured to me that concatination could have hight priority than
arithmetic operations.

Jul 17 '05 #3
aa wrote:
I am traing to output the following HTML string from PHP:
"<td><input name='".$i-2."' size='2' type='text'
onBlur='myfunction(".$i-2.")'></td>"
But it somehow mistreats $i-2. PHP does not throw any errors but the
browser renders the table incorrectly.
I got round this by introducing another variable
j=i-2 and using it in place of i-2
It works. However I am interested to understand why is does not work with
i-1. Any comments?


Hi,

I noticed that problem too.
I guess there is a very valid reason why it happens. :-)

Solve it by using ()
so:
$i=1;
echo "testing i+1=".$i+1; // gives 1

but

echo "testing i+1=".($i+1); // gives 2
Possible reason (as a guru to be sure. :P)
I expect PHP interprets the line as:
$result = (print "i+1=".$i) + 1;
// result contains 2 now!

where:

$result = (print "i+1=".($i + 1);
// result contains 1 now!
Regards,
Erwin Moller
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Marc Le Roy | last post: by
17 posts views Thread by beliavsky | last post: by
17 posts views Thread by Victor Bazarov | last post: by
2 posts views Thread by Martin Magnusson | last post: by
2 posts views Thread by Michael Olea | last post: by
7 posts views Thread by gretean | last post: by
4 posts views Thread by Julek | last post: by
6 posts views Thread by Peter | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.