PDA

Click to See Complete Forum and Search --> : Counter Not Incrementing - What Gives? [RESOLVED]


techgnome
Jul 24th, 2004, 09:57 AM
Ok, I don't think I'm a complete gimp, but I'm proly a partial one.

The background: I want to add a dropcase letter to my website, and I only want it on the first letter of each part, not on every paragraph, so using CSS is out of the question. So, what I'm trying to do is as each section is processed, I grab the content (passed in as $content), grab the first letter and change the markup. The problem is that sometimes the first thing is some HTML. So I want to skip over the HTML, find the first displayable letter, pull it out of the HTML, and then prepend it so that the dropcase is then followed by the rest of the content.

This should (http://techgnome.anderson-website.net/index2.php) give you an idea of what I'm talking about.

So, here's my code that I am using:

$newContent = trim($content);

$ltrPos = 0;
$openTag = '';

$first_letter = substr($newContent,0,1); // Get the first letter

if ($first_letter == '<') {
//The text is marked up and we have an opening tag
//Need to get past that to find the first chr. Should
// be able to find it by searching for the matching >

while ($firstLetter == '<') {
while(substr($newContent,$ltrPos,1) != '>')
{
$ltrPos +=1; // $ltrPos + 1;
}
$openTag = substr($newContent, 0, $ltrPos+1);
$first_letter = substr($newContent,$ltrPos+1,0);
}
$newContent = "<div class='drop-case' id='".$ltrPos."'>".$first_letter."</div>".$openTag.substr($newContent,$cnt+2);
}else {
$newContent = "<div class='drop-case'>".$first_letter."</div>".substr($newContent,1);
}


return $newContent;


From what I can tell, my $ltrPos isn't being incremented. I can tell that it's going into the top part, where $first_letter == '<' but it doesn't seem to be going through either of the while loops.

Can any one see what I may have smegged up?

TG

techgnome
Jul 24th, 2004, 08:14 PM
Turns out I'm an idiot.... I switched variable names part way through.... it's those little things in life... *sigh

TG

ober0330
Jul 26th, 2004, 07:09 AM
*** is this?? $ltrPos +=1; Try:$ltrPos++;

visualAd
Jul 26th, 2004, 08:20 AM
Originally posted by ober0330
*** is this?? $ltrPos +=1; Try:$ltrPos++;
Does it really matter? :rolleyes:

The result is the same both.

ober0330
Jul 26th, 2004, 08:43 AM
No, it doesn't really, but ++ is faster and why would you do +=1 when ++ is available to you? Why reinvent the wheel????

visualAd
Jul 26th, 2004, 09:11 AM
I don't see why there would be a speed difference. Both statements are identical and its not reinventing the wheel is mearly using a different method of achieving the same result.

Maybe tech prefers to use +=1 rather than ++ becuase it makes his code look clearer.

techgnome
Jul 26th, 2004, 09:16 AM
I prefer += simply because that's what I'm used to from other languages. Coinsidently, ober0330 is right ++ is faster than +=..... read that in several places, although no one could explain why, but I think it has something to do with the way the language was compiled.

As a side note, I have gone back and updated the code to use ++.... so far I've not noticed any diff, but sometimes every little bit helps. :wave: :thumb:

TG

CornedBee
Jul 26th, 2004, 01:01 PM
In a scripting language like PHP which is never optimized, ++ is likely to be faster because it's more direct, i.e. it's clear that it means "increment by one", unlike += , where an argument needs to be parsed first.
The difference is minimal though, especially with a free type system like most scripting languages have.