I’ve written a code like following in php
<?php
global $server ,$username ,$password ,$database;
$server ="localhost:3306";
$username="user";
$password="pass";
$database="dbname";
function connectToDB()
{
mysql_connect($server,$username,$password) or die ("can't connect to server");
mysql_select_db($database) or die ("can't select database");
}
?>
but I’m not able to access those variables inside my function what is my problem?
even without the global keyword I’m not able to access those variables.
globalmeans “Get this variable from the global scope” not “Make this variable available in all functions”. You use it inside functions, not outside them.See the example in the documentation