Page 1 of 1

T_Variable

Posted: Fri Sep 17, 2010 9:30 am
by Nodral
Ah, the old Unexpected T_variable chesnut!!!

Apparently on line 24 of this code. I'm stumped!!!

Any ideas guys?

Code: Select all

<?php
function percentCalc($value1,$value2,$value3,$value4,$value5,$total)
{
	$array=array($value1,$value2,$value3,$value4,$value5);
	while (list($key,$arraycontent)= each($array))
	{
		$c_percent=(($arraycontent/$total)*100);
		$final_array[]=$c_percent;
	}
	
	return $final_array;
}

function CleanArray($array)
	{
		foreach($array as $value)
		{
			$pattern='/\w/';
			if ($value == $pattern)
				{
				$arrayout[]=($value);
		}
	}
	reset $arrayout
	return $arrayout;
}


?>

Re: T_Variable

Posted: Fri Sep 17, 2010 10:01 am
by McInfo
This line is missing a semicolon.

Code: Select all

reset $arrayout

Re: T_Variable

Posted: Fri Sep 17, 2010 10:15 am
by Nodral
Done that, Still same error

Re: T_Variable

Posted: Fri Sep 17, 2010 10:25 am
by McInfo
Reset is a function. It needs parentheses.

Code: Select all

reset($array);
Why do you need to reset the array here?

Re: T_Variable

Posted: Fri Sep 17, 2010 1:06 pm
by Nodral
I was taught to reset as good habit.

It seems that $arrayout is not an array. I can guarentee there ave values coming in, but I need to filter out any which are not letters or numbers, hence the function, is there an issue with the if statement?

Re: T_Variable

Posted: Fri Sep 17, 2010 2:04 pm
by McInfo
As long as you are gathering habits... Always initialize variables. For example,

Code: Select all

$arrayout = array();
This line compares two strings. It does not perform a regular expression match.

Code: Select all

if ($value == $pattern)
To filter an array using a regular expression, use preg_grep(). Your entire CleanArray() function can be replaced by a call to preg_grep().

Re: T_Variable

Posted: Mon Sep 20, 2010 3:37 am
by Nodral
Ok, now using the preg_grep function instead of the user defined function I built. However, now I get this error. I can't find a comma in the wrong place in this script. Any ideas? Apparently there is an unexpected , on line 4.
Sorry to be a pain and a complete noob, but this has had me scrathcing my head!!!

Code: Select all

<?php
function percentCalc($value1, $value2, $value3, $value4, $value5, $total)
{
	$array[]=array($value1,$value2,$value3,$value4,$value5);
	while (list($key,$arraycontent)= each($array))
	{
		$c_percent=(($arraycontent/$total)*100);
		$final_array[]=$c_percent;
	}
	
	return $final_array;
}
?>

Re: T_Variable

Posted: Mon Sep 20, 2010 11:38 am
by McInfo
Compare these two scripts. The first has a one-dimensional array with three elements. The other has a two-dimensional array with one element.

Code: Select all

$array = array('a', 'b', 'c');
print_r(each($array));
/*
Array
(
    [1] => a
    [value] => a
    [0] => 0
    [key] => 0
)
*/

Code: Select all

$array[] = array('a', 'b', 'c');
print_r(each($array));
/*
Array
(
    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )
    [value] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )
    [0] => 0
    [key] => 0
)
*/
Why not have a single parameter for percentCalc() -- an array of values?

Is $total the sum of all the values? If so, that calculation can occur inside the function (see array_sum()).