This question evolves around the concept of how we can add <a name=“id here”></a>'s on a web page to allow a visitor to skip to sections of the page without having to scroll down a lot.
I’m thinking about how I could achieve this in PHP. A web page would have various <a name=“id here”></a> scattered throughout its length (the longer the page the more there would be) and PHP would simply go from top to bottom and add a numeric value for each one.
Any ideas how you would tackle this one? The ID’s could be 0, 1, 2, 3 etc. so the $i = $i++ is fine but it’s getting that to paste itself automatically inside of each <a name=“id here”>. I guess I could just put $i = $i++ after each section then <a name=“$i”> however somehow I’m not convinced that’s the professional way to go about it.
I’m thinking along the lines of having a function which spits out an ID then preserving the value for the next time it’s called, in which case it will simply +1 to the preserved value. The preserved value then is deleted once the function is called the last time on that page so if the page is refreshed it would start from 0 or 1 again. As it’s only going to be a few lines of code I don’t think there’s any concern for server CPU time in this case given it will generate the ID’s on each page refresh.
This question is about named anchors, which are for links within the same document. The id attribute is a hook for CSS and DOM manipulation, and can’t be used to create named anchors.
The ++ operator is standard in pretty much all imperative languages, including statically typed ones like Java. It is just a shortcut for
True. I was meaning how you don’t have to declare a variable, make it public/global for this to work in PHP enabling easy ways of doing the same thing.
This question is about named anchors, which are for links within the same document. The id attribute is a hook for CSS and DOM manipulation, and can’t be used to create named anchors.
Guess I haven’t kept up with that, sorry. You’re absolutely right, though I don’t see why “id is recommended today”. The spec you linked recommends name – you can use a wider variety of values and it’s supported by older browsers.
I haven’t used named anchors in a website since the days of GeoCities, when HTML 3.2 was still common, and the name attribute was the only way to do it. That’s why they were called “named anchors”.