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:

<input id="exact" class="exact" name="exact" size="10"/><input type="submit" name="send" value="ApplyExact" />

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:

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;
	}

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:


$_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>');
		}

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?


...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;
	}

or by:


...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;
	}

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

Instead of just returning true when a match is found, why not return some value which indicate where the match was made?

function validateExact($exact)
{
	// if either less than 10 or more than 10, return false
	if(strlen($exact) < 10 || strlen($exact) > 10)
	{
		return false;
	}
	else
	{
		// setup the arrays
		$colour_one = array('red','orange','yellow');
		$colour_two = array('pink','purple');
		$colour_three = array('blue','grey','white');
		
		if ( in_array($exact, $colour_one) )
		{
			return ".30";
		}
		else if ( in_array($exact, $colour_two) )
		{ 
			return ".50";
		}
		else if( in_array($exact, $colour_three) )
		{
			return ".70";
		}
		else
		{
			// if not in any of the arrays, return false.
			return false;
		}
	}
}

That function will return false if no match were found, or if lenght of submitted value is less then 10 or higher than 10.
But if a match is found, the returned value would be “.30”, “.50” or “.70”…

And then use it something like this:

$_SESSION['exact'] = $_POST['exact'];

if( isset($_POST['exact']) )
{
	$type = validateExact($_POST['exact']);
	if($type !== false)
	{
		echo '<tr><td>Your Colour is '.$_SESSION['exact'].' and is valued at '.$type.'</td></tr>';
	}
	else
	{
		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>';
	}
}

Just do:


if (in_array($exact, array('red','orange','yellow'))) {
return .30;
}

For each one. Return tells the function to stop executing and return whatever value you executed.

So when you did return true; $type = .30; the function return the boolean TRUE and stopped right there. Variables within functions are only available within the function. So you will need to do this:


$type = validateExact($_POST['exact']);

if (isset($_POST['exact']) && !$type) {

Also, you don’t want this part in your validateExact function:


        if(strlen($exact) < 10)
            return false;
        if(strlen($exact) >10)
            return false;

strlen returns how many characters (including whitespace) are in a string. If you are passing: red, strlen will equal 3. You told it to terminate if the strlen, 3, is less than 10 or greater than 10, which means it needs to be exactly 10 characters… Your code will never get to the color if statement.

If I were to rewrite your function, I would write it like this:


function validateExact($exact=null) {
        $colours = array(
            'red'         => .30,
            'orange'     => .30,
            'yellow'     => .30,
            'pink'         => .50,
            'purple'     => .50,
            'blue'         => .60,
            'grey'         => .60,
            'white'     => .60,
        );
        
        $exact = strtolower($exact);

        if (isset($colours[$exact])) {
            return $colours[$exact];
        }
        
        return false;
}

That function will always return false.

First you are checking that the length is 10 characters. If it’s less than 10, you are returning false. If it’s more than ten you are also returning false.

Afterwards you are comparing against colours which dont have a length of 10 - in other words, they will never equal $exact AND have 10 characters.

So ignoring the first bit you could just simply do:

function validateExact($exact){
    switch($exact){
        case 'red': case 'orange': case 'yellow':
            return '0.3';
            break;
        case 'pink': case 'purple':
            return '0.5';
        case 'blue': case 'grey': case 'white':
            return '0.7';
            break;
        default:
            return false;
            break;
    }
}
Edit:

Ages behind that post above. Good call, Tmapm!

oops sorry i didn’t mean to put the ‘if(strlen($exact) < 10 etc’ in the validateExact function!! My mistake that was from another function

Thanks for the replies everyone! zalucius i ended up using your code and works just as i wanted, seems obvious now that instead of just returning true i could return the value.

tmapm & Jake Arkinstall thanks as well, all the help i get even if i don’t use the direct code itself is a big help! trying to learn as much as possible as quickly as possible:)

Thanks again!

Jon