Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defaulting variables

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA

Hi,

I would like to know how to effectively default variables.

Code:
$myField = $_POST["Field"];

I want to default the value to 0 if there are no value passed. If have several variables to default and I wanting to find a clean way to achieve this; also by avoiding using if else statement for each variables.

Thanks
 
I don't know of a way to do this without a bunch of if statements or their equivalent (such as the ternary operator link)

You might try setting the defaults as values when you create the HTML....



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi,
you could make a function..
Code:
echo washVariable("");
echo "<hr />";
echo washVariable("test");

washVariable($input="", $washvalue=0){
if (empty($input) || trim($input) == "")) {
    $output = $washvalue;
  }
else {
    $output = $input;
  }
return $output;
}

ps. this is directly from my rotten brain.. it might have a bunch of errors and strange logic, as it's really late in the night here :p

Good night and good luck!

Olav Alexander Mjelde
Admin & Webmaster
 
Sorry, Some terrible coding above by yours truly!

Here is a working version:
Code:
<?php
echo washVariable("");
echo "<hr />";
echo washVariable("test");
echo "<hr />";
echo washVariable("", 3);


function washVariable($input="", $washvalue="0"){
if (empty($input) || trim($input) == "") {
    $output = $washvalue;
  }
else {
    $output = $input;
  }
return $output;
}
?>

I guess it's self explanatory.

Olav Alexander Mjelde
Admin & Webmaster
 
I think DaButcher has a very good idea. I would like the opportunity to embellish on it....

Given the following HTML, from either a static file or a script:

Code:
<html><body>
<form method="post" action="test_foo.php">
<input type="text" name="formfield1"><br>
<input type="text" name="formfield2"><br>
<input type="text" name="formfield3"><br>
<input type="submit">
</form>
</body></html>

and the script test_foo.php:

Code:
<?php
$default_values = array
(
	'formfield1' => 'defaultvalue1', 
	'formfield2' => 'defaultvalue2', 
	'formfield3' => 'defaultvalue3', 
);

function wash ()
{
	global $default_values;
	
	foreach ($default_values as $variable => $value)
	{
		if (!isset($_POST[$variable]) || $_POST[$variable] == '')
		{
			$GLOBALS[$variable] = $value;
		}
		else
		{
			$GLOBALS[$variable] = $_POST[$variable];
		}
	}
}

wash();

print '<html><body><pre>';
print $formfield1 . "\r\n";
print $formfield2 . "\r\n";
print $formfield3 . "\r\n";
print '</pre></body></html>';
?>

The function wash() will instantiate, in the global address space, all the fields from the form with defaults in place if no values are submitted for a field. If, for example, I enter [tt]z[/tt] into the second field and submit, the output of test_foo.php is:

[tt]defaultvalue1
z
defaultvalue3[/tt]

It still uses if-statements, but they are all neatly bound up in a function, as DaButher suggested. All you need to is modify the associative array $default_values so that the fieldname is the key and its default value is the value.

I don't particularly care for creating the extra variables, as $_POST already has the values. (I only use references to the elements of $_POST in my code -- I figure 6 months from now I may not remember how a value got in $foo, but $_POST['foo'] is self-documenting.) But if your style is to create the extra variables, this might be what you're looking for.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I figure 6 months from now I may not remember how a value got in $foo, but $_POST['foo'] is self-documenting.

I know this might be a bit off topic but thought I'd just ask what do you do about:

Notice: Undefined index...
Notice: Undifined variable...

prompts if php is set to display all warnings.

On 1 side of the fence, they say to initialize all your varibles and leave all warnings on. This will make you write better code.


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
On my development sites or machines, I have PHP set to display all warnings, notices, errors, etc. Like you said, if you don't know what you're doing wrong, you can't fix it.

As for the "undefined index" and "undefined variable" errors, if I don't know for a fact that the variable will exist [and I will know the variable will exist when the script creates it outside of a conditional or a loop], then I test for its existence before I use it. This does require extra code, but I come to PHP from c-language, where testing for things before you use it is a way of life.

Now, my production sites have the display of errors turned off, but with the errors logged to the site's error logfile. I don't want users to see the errors, but I need to know about them.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top