Page 1 of 1

Making a variable 'global' [solved] merry christmas! .. eve!

Posted: Sun Dec 23, 2007 8:38 am
by markusn00b
So here's what ive got, it's a validation function:

Code: Select all

<?php
/*
*$username = $_POST['username'];
*$email    = $_POST['email'];
*
*#$usernameExp = '/[^a-zA-Z0-9_]/'; //regExp for username validation
*#$emailExp    = '/^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/'; // ** email validation
*
*if preg_match($emailExp) returns true the email is valid! 
*
*Start if / elseif / else statements to make decision on what file to include
*/

function validateUsername(){
	$username = "mahcuz%%^";  //$_POST['username']; // username POST
	$usernameExp = '/[^a-zA-Z0-9_]/'; //regExp for username validation
	
	if(strlen($username) < 6 || strlen($username) > 16){
		$__error = "Username must be between <em>6</em> and <em>16</em> characters in length.";
		return false;
	}
	elseif(preg_match($usernameExp, $username)){
		$__error = "Username must contain only letters(aA, bB, cC, etc.), numbers(1, 2, 3, etc.) and underscores( _ ).";
		return false;
	} else {
		return true;
	}
}

if(validateUsername()){
	echo "Username is valid.";
} else {
	echo "Username isn't valid: $__error"; 
}
?>
I know there's probably many easier ways to do things but it's what i've written by myself and i'm by no means a good writer.

The problem is have is with the

Code: Select all

$__error
I want to display what error was encountered in the validation.
I've heard about making the variable global? Not too sure.

Any suggestions, thanks.

:)

Posted: Sun Dec 23, 2007 9:02 am
by webspider
use keyword global before variable name inside your function

Code: Select all

function validateUsername(){ 

 global $__error ;


}

Posted: Sun Dec 23, 2007 9:15 am
by markusn00b
Thankyou muchly.

:)

Posted: Sun Dec 23, 2007 9:28 am
by webspider
you are welcome :)

Posted: Sun Dec 23, 2007 10:17 am
by feyd
Use of global(s) is badness. Avoid it.

If you're running PHP 5, throw exceptions. If you're using PHP 4, trigger_error().

Posted: Sun Dec 23, 2007 3:46 pm
by Jonah Bron
How about this?

Code: Select all

function validateUsername(){ 
   $username = "mahcuz%%^";  //$_POST['username']; // username POST 
   $usernameExp = '/[^a-zA-Z0-9_]/'; //regExp for username validation 
    
   if(strlen($username) < 6 || strlen($username) > 16){ 
      return "Username must be between <em>6</em> and <em>16</em> characters in length.";    } 
   elseif(preg_match($usernameExp, $username)){ 
      return "Username must contain only letters(aA, bB, cC, etc.), numbers(1, 2, 3, etc.) and underscores( _ ).";
   } else { 
      return false; 
   } 
} 

if(!validateUsername()){ 
   echo "Username is valid."; 
} else { 
   echo "Username isn't valid: $__error"; 
}

Posted: Mon Dec 24, 2007 8:50 am
by markusn00b
feyd wrote:Use of global(s) is badness. Avoid it.

If you're running PHP 5, throw exceptions. If you're using PHP 4, trigger_error().
Why is this?

Posted: Thu Dec 27, 2007 1:45 pm
by feyd
It makes your code dependent on very specific things existing in the global name space. Since variables will likely change over time, this makes for more painful management. Find an alternate means of communicating the information to methods/functions, such as via a passed parameter.