Link to home
Start Free TrialLog in
Avatar of jezella
jezellaFlag for United Kingdom of Great Britain and Northern Ireland

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($mysqlHost, $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($db, $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
ASKER CERTIFIED SOLUTION
Avatar of Joe Wu
Joe Wu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jezella

ASKER

Nizsmo, this is very good where my interpretation is that this code would run faster also.  This php education is addictive.

Many thanks.

Jezella
Glad to be of assistance :)