My webpage url is http://example.com/blog/
When I echo it is showing the correct result as blog.
echo $_SERVER['REQUEST_URI']
>>blog
But, when I use in if condition
<?php if($_SERVER['REQUEST_URI']== 'blog') echo 'class="Margincls"';?>
it doesn't return anything, tried with basename and strtolower as well
You are using wrong quotations. Copy and paste this:
<?php if($_SERVER['REQUEST_URI']== 'blog') echo 'class="Margincls"';?>
If your url is http://example.com/blog/
, $_SERVER['REQUEST_URI']
should be /blog/
. I checked on my environment - Apache, PHP 5.6.
Reference: http://php.net/manual/en/reserved.variables.server.php
$_SERVER['REQUEST_URI'] also returns the first forward slash, so you need to check this when equating.
<?php if($_SERVER['REQUEST_URI']== '/blog') echo 'class="Margincls"';?>
<?php if($_SERVER['REQUEST_URI']== "blog") echo 'class="Margincls"';?>
Adding double quotes solved the problem.
This should work
<?php if($_SERVER['REQUEST_URI']== '/blog/') echo 'class="Margincls"';?>