[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Define variable methods

Posted on 2007-10-07
3
Medium Priority
?
261 Views
Last Modified: 2007-10-07
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
0
Comment
Question by:jezella
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
nizsmo earned 2000 total points
ID: 20029820
Easy way to resolve this would be to initialize default values for the arguments for the function as below:


function serverConnect($mysqlHost=null, $mysqlUser=null, $mysqlPassword=null)
      {
      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;
            }
      }

This way you can call the function without any arguments: serverConnect();
Then because of this, you do not need these 3 lines:
$mysqlHost    =NULL;
$mysqlUser    =NULL;
$mysqlPassword=NULL;

Please let me know how you got on.
0
 

Author Comment

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

Many thanks.

Jezella
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20030057
Glad to be of assistance :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question