How to switch a php variable according to the server address

I managed to switch my websites links (containing a php variable: $root) according to the server address. If they are in localhost the variable begins with: http://localhost , if they are on remote the variable is the remote address.

The php code is quite simple:

if ($_SERVER['SERVER_NAME'] == "localhost")
{$root_yourwebsiteA = "http://localhost/your path/yourwebsiteA/";
 $root_yourwebsiteB = "http://localhost/your path/yourwebsiteB/";
 $root_yourwebsiteC = "http://localhost/your path/yourwebsiteC/";
}
else 
{$root_yourwebsiteA = "http://remote path/yourwebsiteA/";
 $root_yourwebsiteB = "http://remote path/yourwebsiteB/";
 $root_yourwebsiteC = "http://remote path/yourwebsiteC/";
}

how to pass a php variable to javascript

You can use a code like the following:

<?php 
  $name= "Hello World";
?> 
<script type="text/javascript"> 
  var x = "<?php echo "$name"?>";      
  document.write(x);    
</script>

and then insert this code in a php page: but not with a js extension, but with some like inc