By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Making a variable 'global'.

Markus
Expert 5K+
P: 6,050
This is for you pb - you're looking a little bored; answering all those old threads ;)
So here's what ive got, it's a validation function:
[php]
<?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";
}
?>
[/php]
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
[php]
$__error
[/php]
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.

:)
Dec 23 '07 #1
Share this Question
Share on Google+
1 Reply


Markus
Expert 5K+
P: 6,050
Not sure why the 'edit' button has dissapeared...

Anyway, i've solved it - well someone else did at a different forum.

i just needed to (as i though i would) define it as global

i.e.
[php]
function validateUsername(){
global $__error;
}
[/php]
Dec 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.