I have a small switch statement in the header of a portfolio website I’m working on which governs which links are shown on which page. The value of $id comes from the GET variable, ie – ‘?id=index’.
switch($id) {
case "index":
//Show links to content
case !"index":
//Show link to index
case !"about":
//show link to about page
}
The issue is that the NOT operator isn’t working in the final two cases. I want the link to the index to show when the user is NOT on the index page, and likewise with the about page. Currently, ALL links are shown on the index page (when $id == “index), and NONE are shown on any other pages.
Why might this be?
This is so, because it is supposed to be so.
switchcompares using the==operator. So in the second case, you are actually testing whetherWhich will always evaluate to
falsesince any string would betrueand not true would befalse.Which means, in your case it would be better to use
ifandelse.