jezella
asked on
Define variable methods
To the best of my knowledge the following code works without problems. However, in an attempt to write clean and tidy code that does not result in ANY browser notices what would be the recommended method of defining variable.
The following code includes a function called passwordCheck() where you can see that I have defined the $mysqlxxxx variables as = NULL with in an else clause. Without this I receive notices of undefined variables. Can the positioning and method of defining here be considered as good practice?.
Comments appreciated.
<?php
$db='members';
// Connect to server
function serverConnect($mysqlHost, $mysqlUser, $mysqlPassword)
{
include ('secret.php');
global $conn;
@$conn=mysql_connect($mysq lHost, $mysqlUser, $mysqlPassword);
//return $conn;
if ($conn)
echo "Connected to Server<br />";
else
{
echo "Connection to the server failed.<br />\n
</body>\n
</html>";
exit;
}
}
// Select Database
function selectDb($db, $conn)
{
$connDb=mysql_select_db($d b, $conn);
if (!$connDb)
{
echo 'Sorry, We were Unable to Connect to the Database.';
exit;
}
else
echo "Connected to Database";
}
function passwordCheck($username, $password)
{
global $conn;
global $db;
if (empty($username) OR empty($password))
{
include ('registration-form.php');
echo '<br /><h3 align = "center">Please suppy all info</h3>';
exit;
}
else
{
$mysqlHost =NULL;
$mysqlUser =NULL;
$mysqlPassword=NULL;
serverConnect($mysqlHost, $mysqlUser, $mysqlPassword);
selectDb($db, $conn);
}
}
?>
Jezella
The following code includes a function called passwordCheck() where you can see that I have defined the $mysqlxxxx variables as = NULL with in an else clause. Without this I receive notices of undefined variables. Can the positioning and method of defining here be considered as good practice?.
Comments appreciated.
<?php
$db='members';
// Connect to server
function serverConnect($mysqlHost, $mysqlUser, $mysqlPassword)
{
include ('secret.php');
global $conn;
@$conn=mysql_connect($mysq
//return $conn;
if ($conn)
echo "Connected to Server<br />";
else
{
echo "Connection to the server failed.<br />\n
</body>\n
</html>";
exit;
}
}
// Select Database
function selectDb($db, $conn)
{
$connDb=mysql_select_db($d
if (!$connDb)
{
echo 'Sorry, We were Unable to Connect to the Database.';
exit;
}
else
echo "Connected to Database";
}
function passwordCheck($username, $password)
{
global $conn;
global $db;
if (empty($username) OR empty($password))
{
include ('registration-form.php');
echo '<br /><h3 align = "center">Please suppy all info</h3>';
exit;
}
else
{
$mysqlHost =NULL;
$mysqlUser =NULL;
$mysqlPassword=NULL;
serverConnect($mysqlHost, $mysqlUser, $mysqlPassword);
selectDb($db, $conn);
}
}
?>
Jezella
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Glad to be of assistance :)
ASKER
Many thanks.
Jezella