/    Sign up×
Community /Pin to ProfileBookmark

php variable ‘return’ help

Hey

I have an simple form set up in my ‘main.php’ page with one field causing me problems which is below:

[code]<input id=”exact” class=”exact” name=”exact” size=”10″/><input type=”submit” name=”send” value=”ApplyExact” />[/code]

So the once the user enters their input in the ‘exact’ field they submit the form. I have a ‘require_once(“validate.php”); set up that links a php file which has the following function to validate the input to check if it matches one of the exact terms in either of the 3 arrays:

[code]function validateExact($exact){
if(strlen($exact) < 10)
return false;
if(strlen($exact) >10)
return false;
$colour_one = array(‘red’,’orange’,’yellow”);
$colour_two = array(‘pink’,’purple’);
$colour_three = array(‘blue’,’grey’,’white’);
if ( in_array($exact, $colour_one) )
return true;
if ( in_array($exact, $colour_two) )
return true;
if ( in_array($exact, $colour_three) )
return true;
else
return false;
}[/code]

Now back in my ‘main.php’ page once the submit button has been pressed the user gets taken to the ‘ApplyExact’ case which has the following code:

[code]
$_SESSION[‘exact’] = $_POST[‘exact’];

if( isset($_POST[‘exact’]) && (!validateExact($_POST[‘exact’]))) {
echo (‘<tr><td><td>The Colour is Invalid</td></tr><tr><tr><td>Colour<input id=”exact” class=”exact” name=”exact” size=”10″/><input type=”submit” name=”send” value=”ApplyExact” /></td></tr>’);
}
else {
echo (‘<tr><td>Your Colour is ‘.$_SESSION[‘exact’].’ and is valued at ‘.$type.'</td></tr>’);
}[/code]

Now this all works fine and i get the users colour choice echoed back if it is stored in one of the arrays but the problem i have is that i cannot get the variable of ‘$type’ to be displayed. I need to set a variable of ‘type’ – which value depends on what colour array the user selected.

So for example if they enter any of red, orange or yellow from the array ‘$colour_one’ then
$type = .30.

If they enter pink or purple from the array ‘$colour_two’ then
$type = .50 and so on…

I have tried numerous different attempts at getting this working including the below which i thought should work but isn’t. Are the 2 examples below completely wrong and i need another way around this or am i missing something?

[code]
…if ( in_array($exact, $colour_one) )
return true;
$type = .30;
if ( in_array($exact, $colour_two) )
return true;
$type = .50;
if ( in_array($exact, $colour_three) )
return true;
$type = .60;
else
return false;
}[/code]

or by:

[code]
…if ( in_array($exact, $colour_one) )
$type = .30;
$true = true && $type;
return $true;
if ( in_array($exact, $colour_two) )
$type = .50;
$true = true && $type;
return $true;
if ( in_array($exact, $colour_three) )
$type = .60;
$true = true && $type;
return $true;
else
return false;
}[/code]

If anyone can help with this?

I have only posted the sections of the code that relate to my problem and question as the whole script for ‘main.php’ is about 1200 lines long with ‘validate.php’ 140 lines so tried to make it a bit clearer and easier.

Many thanks
Jon

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 19.2010 — I would suggest returning the "type" value from the function, thus avoiding either having to define $type as global in the function (generally considered bad technique for a number of reasons) or else passing it by reference as one of the function arguments (not a bad solution, but I would only use it if it is really necessary to separate it from the function's return value).
[code=php]
function whatever('arg list here')
{
// define arrays, etc., then...
$type = false;
if(in_array($exact, $colour_one))
{
$type = .30;
}
elseif(in_array($exact, $colour_two))
{
$type = .50;
}
elseif(in_array($exact, $colour_three))
{
$type = .60;
}
return $type;
}
[/code]

Then when calling the function, you can save the return value to whatever variable you want to use, and check it for false to see if it failed:
[code=php]
$type = whatever('args go here');
if($type === false)
{
// handle error condition here
}
[/code]
×

Success!

Help @oddball25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...