I have the following JS:
if(window.location.href.indexOf("search.php") != -1
|| window.location.href.indexOf("list.php") != -1
|| window.location.href.indexOf("view.php") != -1
|| window.location.href.indexOf("contact.php") != -1) {
But want to convert it to PHP. What is the equivalent of indexOf in PHP or the best way to do this in PHP.
I don’t understand the strpos examples people are linking to below. Perhaps an example more in line with what I have done in JS?
Although your JavaScript code is using
indexOf(), PHP actually has a better way of handling what you want to achieve.You simply have to check the script name in your URL, then perform an action(s) based on it.
In my opinion, PHP
switchstatement is better than if statements in combination with PHPstrpos()function.Below is an illustration of how you can achieve that using a PHP switch statement: