Behaviour of NULL Parameter

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
leeladhar
Forum Newbie
Posts: 3
Joined: Wed May 31, 2006 4:36 am
Location: Bangalore,India

Behaviour of NULL Parameter

Post by leeladhar »

twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, Everybody

This is the kind of problem I faced when I was checking for the error messages. This is the code

Code: Select all

//Validation of fields entered if js is dsabled

			$error_msg = array();

				if(($str = $val[0]) || ($str = $val[1]))
				{
					$error_msg[]  = (validate::name($str)) ? NULL : 'Enter the Name field properly';
				}
				else
				{
					$error_msg[]  = 'First name or last name cannot be empty';
				}
				if($val[2])
				{
					$error_msg[]  =  (validate::name($val[2])) ? NULL : 'Enter the Address field properly';
				}
				if($val[3])
				{
					$error_msg[]  =  (validate::name($val[3])) ? NULL : 'Enter the City field properly';
				}
				if($val[4])
				{
					 $error_msg[] = (validate::name($val[4])) ? NULL :'Enter the State properly';
				}
				if($val[5])
				{
					$error_msg[] = (validate::number($val[5])) ? NULL : 'Enter the Zip code properly';
				}
				
				if($val[7])
				{
					$error_msg[] = (validate::number($val[7])) ? NULL : 'Enter the Telephone number properly';
				}
				else
				{
					$error_msg[] = 'Telephone number field cannot be empty';
				}
				//print("<pre>");print_r($error_msg);die();
				if(count($error_msg)>0)
				{
					$error_str = implode('.', $error_msg);
					print($error_str);
				}



				
			//end of validation
In the above code "val" array contains the values of input fields, and I am checking for input validation.

In this expression, (exp1) ? NULL : (error display string);, If there are some errors then the validation functions return false and , the value returned to array is err string else NULL is returned, Finally at last I am checking whether the array size is greater than one then display the error string.

But even though , there is no input with errors, still the array contains elements all initialized to NULL, Hence enters the final if condition with displaying a null condition, Which I was not expecting to happen..........., Finally solved this by having below code


Can any one explain, How can we avoid this null assignment...what could be the reason


twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

leeladhar wrote:Can any one explain, How can we avoid this null assignment...what could be the reason
Don't use a ternary. It's as simple as that. A null is still a value. Since you are always appending to the array, you're always adding elements, null or strings alike. Pretty simple.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ternary statements can be nasty.

I've seen code that almost made me vomit (ok I'm kidding) where the developer seemed to have lots his marbles and done things like this.

Code: Select all

$foo = $x == 5 ? ($y == 10 ? ($z == 13 ? 7 : true) : 42) : false;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why not use an empty string ('') instead of NULL?
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post by Verminox »

d11wtq wrote:Ternary statements can be nasty.

I've seen code that almost made me vomit (ok I'm kidding) where the developer seemed to have lots his marbles and done things like this.

Code: Select all

$foo = $x == 5 ? ($y == 10 ? ($z == 13 ? 7 : true) : 42) : false;
They get even worse if you are checking for a false value and the code to be executed if the statement returns true is to the right of the colon. For example,

Code: Select all

$foo = $x == false ? ($y != 10 ? ($z === null ? false : true) : 42) : 0;
:D



leeladhar, I can give you a couple of suggestions:


If validate::name($str) can be editted, then edit it so that it takes a second param, which is a referance the errors array, and a third which will be the error message. For example:

Code: Select all

<?php
function name($str, &$errors,$message=''){
	//Do the validation here
	 if ( $valid ){
		return true;
	} else {
		$errors[] = $message;
		return false;
	}
}
?>

Alternatively, you can use the the code that you made and before using count($error_msg) you put this in:

Code: Select all

<?php
foreach ( $error_msg as $i => $e ){
	if($e === NULL){
		unset($error_msg[$i]);
	}
}
?>


Edit: Ooops, I did not realize that this topic was an old one. I don't remember how I got here. Sorry for the unnecessary bump.
Post Reply