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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

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

Post 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.

:)
Last edited by markusn00b on Mon Dec 24, 2007 10:51 am, edited 1 time in total.
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post by webspider »

use keyword global before variable name inside your function

Code: Select all

function validateUsername(){ 

 global $__error ;


}
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post by markusn00b »

Thankyou muchly.

:)
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post by webspider »

you are welcome :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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().
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post 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"; 
}
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply