
WHILE loop problems
I'm having trouble setting up a while loop, and I think I'm creating
overlapping brackets. I'm trying to set up a script to check for up to 3
images to display on a profile page, then put a notice at the bottom. My
initial efforts either get a blank page or the notice appearing a few
hundred times. Below is my original (working) script to display a single
image. After that is my attempts at adding a while loop. Is there a better
way of doing this??
Thanks,
Wm
Original/working version:
//start imagename function
$imagename = "$lastname$firstname";
$imagename = str_replace(" ", "","$imagename");
//end imagename function
//$filename = '../images/$imagename.jpg';
$image_info =
[at] getimagesize("http://www.mydomain.org/images/$imagename.jpg ");
$type=$image_info[2];
if ($type == 0) {
// file_exists(realpath($filename)); {
print " ";
} else {
print "<IMG SRC=\"../images/$imagename.jpg\"><BR>";
print "<FONT color=\"#000000\" face=\"Arial, Helvetica, sans-serif\"
size=\"1\">
© $firstname $lastname. All Rights Reserved.</FONT>";
}
Trying to add a loop:
//start imagename function
//begin new code
$imagenum=1;
while ($imagenum<4) {
//end new code
$imagename = "$lastname$firstname$imagenum";
$imagename = str_replace(" ", "","$imagename");
//end imagename function
//$filename = '../images/$imagename.jpg';
$image_info =
[at] getimagesize("http://www.mydomain.org/images/$imagename.jpg ");
$type=$image_info[2];
if ($type == 0) {
// file_exists(realpath($filename)); {
print " ";
} else {
print "<IMG SRC=\"../images/$imagename.jpg\"><BR>";
$imagenum = $imagenum++;
} // end while loop
print "<FONT color=\"#000000\" face=\"Arial, Helvetica, sans-serif\"
size=\"1\">
© $firstname $lastname. All Rights Reserved.</FONT>";
}
Re: WHILE loop problems
Shooter wrote:
> I'm having trouble setting up a while loop, and I think I'm creating
> overlapping brackets. I'm trying to set up a script to check for up to 3
> images to display on a profile page, then put a notice at the bottom. My
> initial efforts either get a blank page or the notice appearing a few
> hundred times. Below is my original (working) script to display a single
> image. After that is my attempts at adding a while loop. Is there a better
> way of doing this??
>
> Thanks,
> Wm
>
>
> Original/working version:
>
> //start imagename function
> $imagename = "$lastname$firstname";
> $imagename = str_replace(" ", "","$imagename");
> //end imagename function
> //$filename = '../images/$imagename.jpg';
> $image_info =
> [at] getimagesize("http://www.mydomain.org/images/$imagename.jpg ");
> $type=$image_info[2];
> if ($type == 0) {
> // file_exists(realpath($filename)); {
> print " ";
> } else {
> print "<IMG SRC=\"../images/$imagename.jpg\"><BR>";
> print "<FONT color=\"#000000\" face=\"Arial, Helvetica, sans-serif\"
> size=\"1\">
> © $firstname $lastname. All Rights Reserved.</FONT>";
> }
>
>
>
>
> Trying to add a loop:
>
> //start imagename function
> //begin new code
> $imagenum=1;
> while ($imagenum<4) {
> //end new code
> $imagename = "$lastname$firstname$imagenum";
> $imagename = str_replace(" ", "","$imagename");
> //end imagename function
> //$filename = '../images/$imagename.jpg';
> $image_info =
> [at] getimagesize("http://www.mydomain.org/images/$imagename.jpg ");
> $type=$image_info[2];
> if ($type == 0) {
> // file_exists(realpath($filename)); {
> print " ";
> } else {
> print "<IMG SRC=\"../images/$imagename.jpg\"><BR>";
> $imagenum = $imagenum++;
> } // end while loop
> print "<FONT color=\"#000000\" face=\"Arial, Helvetica, sans-serif\"
> size=\"1\">
> © $firstname $lastname. All Rights Reserved.</FONT>";
> }
>
>
>
At least one of your problems here is:
$imagenum = $imagenum++;
This should simply be:
$imagenum++;
Or
$imagenum += 1;
In your $imagenum=$imagenum++ $imagenum will always end up as '1' due to
operator precedence.
You could also replace the while with a for loop:
for ( $imagenum=0; $imagenum<4; $imagenum++ {
//code here
}
Re: WHILE loop problems
Post removed (X-No-Archive: yes)
Re: WHILE loop problems
| > $imagename = "$lastname$firstname$imagenum";
| > $imagename = str_replace(" ", "","$imagename");
sweet jesus! look at the 'magic number' shit going on there! i heard james
macdermott got very pissed off when he saw the picture of james mac dermott
instead of his own when he logged in...the other james was a 300 lb
hermaphrodite posing nude - bald head inclusive. similar tragedy befell stu
artcrossing...stuart crossing looked nothing like the statuesque stu.
don't add to the stigma that php developers are just hacks...as for some of
us, the title is wholly undeserved.
Re: WHILE loop problems
"Tyno Gendo" <user [at] example.com> wrote in message
news:kQeSh.57131$_Q.54208 [at] fe2.news.blueyonder.co.uk...
> }
I think perhaps ++$imagenum would be more appropriate than $imagenum++.
I can't see any purpose in post-incrementing the value here.
But I am still a newbie to PHP.
Vince
Re: WHILE loop problems
"Shooter" <SDshooter [at] hotmail.com> wrote in message
news:46196360$0$5759$4c368faf [at] roadrunner.com...
> I'm having trouble setting up a while loop, and I think I'm creating
> overlapping brackets. I'm trying to set up a script to check for up to 3
> images to display on a profile page, then put a notice at the bottom. My
> initial efforts either get a blank page or the notice appearing a few
> hundred times. Below is my original (working) script to display a single
> image. After that is my attempts at adding a while loop. Is there a better
> way of doing this??
> Trying to add a loop:
>
> //start imagename function
> //begin new code
> $imagenum=1;
> while ($imagenum<4) {
> //end new code
> $imagename = "$lastname$firstname$imagenum";
> $imagename = str_replace(" ", "","$imagename");
> //end imagename function
> //$filename = '../images/$imagename.jpg';
> $image_info =
> [at] getimagesize("http://www.mydomain.org/images/$imagename.jpg ");
> $type=$image_info[2];
> if ($type == 0) {
> // file_exists(realpath($filename)); {
> print " ";
> } else {
> print "<IMG SRC=\"../images/$imagename.jpg\"><BR>";
> $imagenum = $imagenum++;
> } // end while loop
> print "<FONT color=\"#000000\" face=\"Arial, Helvetica, sans-serif\"
> size=\"1\">
> © $firstname $lastname. All Rights Reserved.</FONT>";
> }
>
Sorry to highjack the OP.
I find the above very curious personaly.
I could be wrong, but I thought "++" had a higher precedence than "=", in
fact I'm very sure it does.
However, having said that, "$imagenum++" is a post-increment, which would
suggest that although the "++" operator has higher precedence the
incrementation should occure after the assignment.
Although I have never used a construct such as $val = $val++, I would have
thought that after the assignment, the next operation would have been the
increment. If that were actualy true then I would expect the value to have
incremented by one before the next loop iteration regardless.
A test shows it doesn't, so I'm wrong.
Trying to actualy work out what is going on in the expression in question
makes my brain hurt. It seems that the increment is being discarded
altogether.
Is this actualy an example of undefined behaviour?
Vince Morgan
Re: WHILE loop problems
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:461ad85c$0$16553$afc38c87 [at] news.optusnet.com.au...
| "Shooter" <SDshooter [at] hotmail.com> wrote in message
| news:46196360$0$5759$4c368faf [at] roadrunner.com...
| > I'm having trouble setting up a while loop, and I think I'm creating
| > overlapping brackets. I'm trying to set up a script to check for up to 3
| > images to display on a profile page, then put a notice at the bottom. My
| > initial efforts either get a blank page or the notice appearing a few
| > hundred times. Below is my original (working) script to display a single
| > image. After that is my attempts at adding a while loop. Is there a
better
| > way of doing this??
|
| > Trying to add a loop:
| >
| > //start imagename function
| > //begin new code
| > $imagenum=1;
| > while ($imagenum<4) {
| > //end new code
| > $imagename = "$lastname$firstname$imagenum";
| > $imagename = str_replace(" ", "","$imagename");
| > //end imagename function
| > //$filename = '../images/$imagename.jpg';
| > $image_info =
| > [at] getimagesize("http://www.mydomain.org/images/$imagename.jpg ");
| > $type=$image_info[2];
| > if ($type == 0) {
| > // file_exists(realpath($filename)); {
| > print " ";
| > } else {
| > print "<IMG SRC=\"../images/$imagename.jpg\"><BR>";
| > $imagenum = $imagenum++;
| > } // end while loop
| > print "<FONT color=\"#000000\" face=\"Arial, Helvetica, sans-serif\"
| > size=\"1\">
| > © $firstname $lastname. All Rights Reserved.</FONT>";
| > }
| >
| Sorry to highjack the OP.
| I find the above very curious personaly.
| I could be wrong, but I thought "++" had a higher precedence than "=", in
| fact I'm very sure it does.
i believe it does.
| However, having said that, "$imagenum++" is a post-increment, which would
| suggest that although the "++" operator has higher precedence the
| incrementation should occure after the assignment.
no...i think the operation as a whole is considered before the = ... so, ++
would increment then the assignment would be made (lhs = rhs). afaicr. you
can further test this by ++$imagenum...the pre/post incrementor should have
NO impact on the final lhs result if this precedence processing is true.
| Although I have never used a construct such as $val = $val++, I would have
| thought that after the assignment, the next operation would have been the
| increment. If that were actualy true then I would expect the value to
have
| incremented by one before the next loop iteration regardless.
| A test shows it doesn't, so I'm wrong.
| Trying to actualy work out what is going on in the expression in question
| makes my brain hurt. It seems that the increment is being discarded
| altogether.
| Is this actualy an example of undefined behaviour?
no, however creating such shitty code is well defined, seen all over the
place in various forms and flavors, and the author should be shot. ;^)
Re: WHILE loop problems
Vince Morgan wrote:
> "Shooter" <SDshooter [at] hotmail.com> wrote in message
> news:46196360$0$5759$4c368faf [at] roadrunner.com...
>> I'm having trouble setting up a while loop, and I think I'm creating
>> overlapping brackets. I'm trying to set up a script to check for up to 3
>> images to display on a profile page, then put a notice at the bottom. My
>> initial efforts either get a blank page or the notice appearing a few
>> hundred times. Below is my original (working) script to display a single
>> image. After that is my attempts at adding a while loop. Is there a better
>> way of doing this??
>
>> Trying to add a loop:
>>
>> //start imagename function
>> //begin new code
>> $imagenum=1;
>> while ($imagenum<4) {
>> //end new code
>> $imagename = "$lastname$firstname$imagenum";
>> $imagename = str_replace(" ", "","$imagename");
>> //end imagename function
>> //$filename = '../images/$imagename.jpg';
>> $image_info =
>> [at] getimagesize("http://www.mydomain.org/images/$imagename.jpg ");
>> $type=$image_info[2];
>> if ($type == 0) {
>> // file_exists(realpath($filename)); {
>> print " ";
>> } else {
>> print "<IMG SRC=\"../images/$imagename.jpg\"><BR>";
>> $imagenum = $imagenum++;
>> } // end while loop
>> print "<FONT color=\"#000000\" face=\"Arial, Helvetica, sans-serif\"
>> size=\"1\">
>> © $firstname $lastname. All Rights Reserved.</FONT>";
>> }
>>
> Sorry to highjack the OP.
> I find the above very curious personaly.
> I could be wrong, but I thought "++" had a higher precedence than "=", in
> fact I'm very sure it does.
> However, having said that, "$imagenum++" is a post-increment, which would
> suggest that although the "++" operator has higher precedence the
> incrementation should occure after the assignment.
> Although I have never used a construct such as $val = $val++, I would have
> thought that after the assignment, the next operation would have been the
> increment. If that were actualy true then I would expect the value to have
> incremented by one before the next loop iteration regardless.
> A test shows it doesn't, so I'm wrong.
> Trying to actualy work out what is going on in the expression in question
> makes my brain hurt. It seems that the increment is being discarded
> altogether.
> Is this actualy an example of undefined behaviour?
> Vince Morgan
>
>
I thought same as you Vince, but seemingly it doesn't leave it
incremented. I've never used that kind of assignment either.
Re: WHILE loop problems
"Steve" <no.one [at] example.com> wrote in message
news:WIDSh.101$Xq6.16 [at] newsfe12.lga...
> no...i think the operation as a whole is considered before the = ... so,
++
> would increment then the assignment would be made (lhs = rhs). afaicr. you
> can further test this by ++$imagenum...the pre/post incrementor should
have
> NO impact on the final lhs result if this precedence processing is true.
Steve, run the following and then look again at the original OP's
expression.
You've got the experience to work this out I think.
$i = 1;
echo $i++.'<br>';
echo $i;
The brain hurt isn't going away just yet ; )
Vince
Re: WHILE loop problems
"Tyno Gendo" <user [at] example.com> wrote in message
news:w9HSh.76255$%g3.50879 [at] fe3.news.blueyonder.co.uk...
> I thought same as you Vince, but seemingly it doesn't leave it
> incremented. I've never used that kind of assignment either.
A simple test of post increment shows that it works as expected, but not in
combination with a previous self assignment.
Last time I saw something like this it was ++var += var++ in C, and that
most certainly produced undefined behaviour.
I wouldn't have a clue how the php interpreter deals with such stuff.
Very curious.
Vince
Re: WHILE loop problems
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:461b8776$0$16556$afc38c87 [at] news.optusnet.com.au...
|
| "Steve" <no.one [at] example.com> wrote in message
| news:WIDSh.101$Xq6.16 [at] newsfe12.lga...
|
| > no...i think the operation as a whole is considered before the = ... so,
| ++
| > would increment then the assignment would be made (lhs = rhs). afaicr.
you
| > can further test this by ++$imagenum...the pre/post incrementor should
| have
| > NO impact on the final lhs result if this precedence processing is true.
|
| Steve, run the following and then look again at the original OP's
| expression.
| You've got the experience to work this out I think.
oh how mistaken you probably are! ;^)
however...it doesn't have to do with precedence so much as state. notice:
$m = 0
for ($i = 0; $i < 10; $i++)
{
$m = $m++;
echo '<pre>' . $m . '</pre>';
}
that will return ten zeros. changing this line:
$m = ++$m;
will return 1 though 10.
logically interpreting the former however, we should be able to express $m =
$m++ like this:
$m = $m;
$m++;
which is why it's so retarded to write it like that in the first place! but,
i digress...
essentially our 'bug' is this, that $m = $m++ nullifies the incrementation.
the lhs of $m makes the rhs of $m++ impossible...$m can only be incremented
if $m is known...thus, it remains 0 all the time. the two seem to be
dependent on each other.
clear as mud? why then does the pre-incrementor work? because ++$m considers
$m BEFORE the rhs operation sets the lhs variable. so, all is known...at all
times. however in $m = $m++, the lhs $m is pending a value assignment and
$m++ only increments after the line $m = $m++ is executed...it's a catch 22.
are you thinking, 'BAD PHP! BAD! BAD!'? well...try this:
<script type="text/javascript">
var m = 0;
var htm = '';
for (var i = 0; i < 10; i++)
{
m = m++;
htm += '<pre>' + m + '</pre>';
}
document.write(htm);
</script>
;^)
Re: WHILE loop problems
"Steve" <no.one [at] example.com> wrote in message
news:xQNSh.14$yM5.9 [at] newsfe02.lga...
> clear as mud? why then does the pre-incrementor work? because ++$m
considers
> $m BEFORE the rhs operation sets the lhs variable. so, all is known...at
all
> times. however in $m = $m++, the lhs $m is pending a value assignment and
> $m++ only increments after the line $m = $m++ is executed...it's a catch
22.
>
> are you thinking, 'BAD PHP! BAD! BAD!'? well...try this:
Nah, it's the OP that I was thinking that of actualy ;)
Thank you, very much appreciated!
Re: WHILE loop problems
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:461c0ab1$0$23675$afc38c87 [at] news.optusnet.com.au...
| "Steve" <no.one [at] example.com> wrote in message
| news:xQNSh.14$yM5.9 [at] newsfe02.lga...
|
| > clear as mud? why then does the pre-incrementor work? because ++$m
| considers
| > $m BEFORE the rhs operation sets the lhs variable. so, all is known...at
| all
| > times. however in $m = $m++, the lhs $m is pending a value assignment
and
| > $m++ only increments after the line $m = $m++ is executed...it's a catch
| 22.
| >
| > are you thinking, 'BAD PHP! BAD! BAD!'? well...try this:
|
| Nah, it's the OP that I was thinking that of actualy ;)
| Thank you, very much appreciated!
no problem. (i hope my guess is right though.)
:)
Re: WHILE loop problems
> I think perhaps ++$imagenum would be more appropriate than $imagenum++.
> I can't see any purpose in post-incrementing the value here.
> But I am still a newbie to PHP.
It makes absolutely no difference in the current context, it is not getting
assigned to anything bar iteself so the end result is the same.
Re: WHILE loop problems
"peter" <submit [at] flexiwebhost.com> wrote in message
news:evi8b2$idm$1 [at] aioe.org...
|> I think perhaps ++$imagenum would be more appropriate than $imagenum++.
| > I can't see any purpose in post-incrementing the value here.
| > But I am still a newbie to PHP.
|
| It makes absolutely no difference in the current context, it is not
getting
| assigned to anything bar iteself so the end result is the same.
it makes ALL the difference. the proof is using ++$imagenum rather than
$imagenum++. even if this were true and all there was to it, there is
incrementation involved. $imagenum after the assignment should be 1 more
than it was before the assignment! and in a loop, each pass would be 1
greater than the last. as it is, it remains the same always.
++$imagenum considers the current value of $imagenum before the
right-hand-side operation, increments, and then sets the left-hand-side
variable ($imagenum). this will return correct/expected results.
$imagenum++ considers the value of $imagenum AFTER the rhs operation,
however since $imagenum IS the lhs variable being set, it cannot be
determined to what $imagenum should be set from the rhs operation...so,
neither operation is successful. the assignment and rhs post-increment
effectively cancel eachother out.
it is not just assigning itself to itself...that would be $imagenum =
$imagenum. it is also incrementing itself after the assignment. logically,
it is:
$i = $i;
$i++;
however, it is written such that the value of $i is indeterminate...so
what's there to increment...or to what?
Re: WHILE loop problems
> it makes ALL the difference. the proof is using ++$imagenum rather than
> $imagenum++. even if this were true and all there was to it, there is
> incrementation involved. $imagenum after the assignment should be 1 more
> than it was before the assignment! and in a loop, each pass would be 1
> greater than the last. as it is, it remains the same always.
>
> ++$imagenum considers the current value of $imagenum before the
> right-hand-side operation, increments, and then sets the left-hand-side
> variable ($imagenum). this will return correct/expected results.
>
> $imagenum++ considers the value of $imagenum AFTER the rhs operation,
> however since $imagenum IS the lhs variable being set, it cannot be
> determined to what $imagenum should be set from the rhs operation...so,
> neither operation is successful. the assignment and rhs post-increment
> effectively cancel eachother out.
>
> it is not just assigning itself to itself...that would be $imagenum =
> $imagenum. it is also incrementing itself after the assignment. logically,
> it is:
>
> $i = $i;
> $i++;
>
> however, it is written such that the value of $i is indeterminate...so
> what's there to increment...or to what?
I was referring to the way it was used by the previous op. The line in the
code was $imagenum = $imagenum++; and someone advised him to use
$imagenum++; instead and in the way it was being used it did not make any
difference. If it had been used in the while statement and not in it's body
then yes it would have made a difference but in this case it did not as I
stated.
Re: WHILE loop problems
peter wrote:
>> it makes ALL the difference. the proof is using ++$imagenum rather than
>> $imagenum++. even if this were true and all there was to it, there is
>> incrementation involved. $imagenum after the assignment should be 1 more
>> than it was before the assignment! and in a loop, each pass would be 1
>> greater than the last. as it is, it remains the same always.
>>
>> ++$imagenum considers the current value of $imagenum before the
>> right-hand-side operation, increments, and then sets the left-hand-side
>> variable ($imagenum). this will return correct/expected results.
>>
>> $imagenum++ considers the value of $imagenum AFTER the rhs operation,
>> however since $imagenum IS the lhs variable being set, it cannot be
>> determined to what $imagenum should be set from the rhs operation...so,
>> neither operation is successful. the assignment and rhs post-increment
>> effectively cancel eachother out.
>>
>> it is not just assigning itself to itself...that would be $imagenum =
>> $imagenum. it is also incrementing itself after the assignment. logically,
>> it is:
>>
>> $i = $i;
>> $i++;
>>
>> however, it is written such that the value of $i is indeterminate...so
>> what's there to increment...or to what?
>
> I was referring to the way it was used by the previous op. The line in the
> code was $imagenum = $imagenum++; and someone advised him to use
> $imagenum++; instead and in the way it was being used it did not make any
> difference. If it had been used in the while statement and not in it's body
> then yes it would have made a difference but in this case it did not as I
> stated.
>
>
A discussion relating to this has been going on in comp.lang.php (where
I made a fool of myself, but hey, we are all learning ;) )
Anyhows, turns out
$imagenum = $imagenum++
The ++ operation returns the variables original value, operating much
like a function, see PHP manual pages:
http://www.phpbuilder.com/manual/en/language.operators.incre ment.php
$a++ Post-increment Returns $a, then increments $a by one.
The key bit is that the increment first "returns $a"
Then the increment operator seemingly doesn't affect the $imagenum you
have assigned to.
So to me, that says that rather then return the result returns is a
"copy" of the original $imagenum's original value, not a reference to
$imagenum, and the ++ is effectively lost.
In short, don't bother to try and understand it, just do $imagenum+=1
instead.
Re: WHILE loop problems
"peter" <submit [at] flexiwebhost.com> wrote in message
news:evklbr$u2u$1 [at] aioe.org...
> I was referring to the way it was used by the previous op. The line in the
> code was $imagenum = $imagenum++; and someone advised him to use
> $imagenum++; instead and in the way it was being used it did not make any
> difference. If it had been used in the while statement and not in it's
body
> then yes it would have made a difference but in this case it did not as I
> stated.
>
>
What I worte was as follows;
>I think perhaps ++$imagenum would be more appropriate than $imagenum++.
>I can't see any purpose in post-incrementing the value here.
>But I am still a newbie to PHP.
>Vince
My reasoning is to do with normal usage, nothing more.
You are certainly right, the outcome is going to be the same regardless, if
used correctly within the body of the loop. But you clearly missed the
point.
"perhaps ++$imagenum would be more appropriate" simply meant to convey that
if you are not requiring side effects then the usage of pre-incrementation
is better IMHO as it's usage is immediately obvious when you, or especialy
someone else, is later reading the code.
If I come across code that uses a post-increment I begin to look for "why"
it was used.
It's a simple matter of semantics, nothing more.
"More appropriate" isn't the same as "should be" either, in case I need
later to explain that too.
Regards,
Vince
PHP » alt.php » WHILE loop problems