tags:

views:

200

answers:

5

I am getting this error: "Fatal error: Can't use function return value in write context in D:\Programas\wamp\www\away\index.php on line 18". Line 18 being the if statement.

Can anyone help me out on this? Thanks.

$vars = array("first_date_month", "first_date_day", "last_date_month", "last_date_day", "resume_date_month", "resume_date_day", "pay_date_month", "pay_date_day", "pay_time_hour", "pay_time_minutes");

$err_flag = false;
$i = 0;
while ($i < count($vars) and $err_flag == false)
{
    if ( (!isset($_GET($vars[$i])) or ($_GET[$vars[$i] == "0") )
     $err_flag = true;
    $i++;  
}
+8  A: 

Maybe I'm not seeing well, but:

if ( (!isset($_GET($vars[$i])) or ($_GET[$vars[$i] == "0") )

You got a really awful mixup of parenthesis and square brackets. There's no such thing as

$_GET()

Big typo you have to correct.

Daniel S
+4  A: 

Your code is a mess.

$_GET is an associative array and not a function (you are using the function call syntax passing $vars[$i] as an argument). In the second $_GET there's one ] missing.

Line 18 should be:

if ( (!isset($_GET[$vars[$i]]) or ($_GET[$vars[$i]] == "0") )
rogeriopvl
A: 

$_GET is a variable, an array -- and not a function.

It means you have to use array-access, with [], to get the data it contains.

So :

$_GET[$vars[$i]]

instead of

$_GET($vars[$i])

for the first time you are using $_GET.


And, for the second time, you forgot to close one ] ; which means you need to use :

$_GET[$vars[$i]]

instead of

$_GET[$vars[$i]


In the end, you while-loop should look like this :

while ($i < count($vars) and $err_flag == false)
{
    if ( !isset($_GET[$vars[$i]]) or $_GET[$vars[$i]] == "0" ) {
        $err_flag = true;
    }
    $i++;  
}

Note I also added {} arround the body of the if condition ; this way, if you ever have to add something, you won't risk forgetting those ;-)

Pascal MARTIN
A: 

Change your line 18 if... to:

if ( (!isset($vars[$i])) or ($vars[$i] == "0") )
    $err_flag = true;
$i++;

This mainly because I -and this is purely personal, I suspect- don't like using the $_GET[...] throughout the script; assign:

$variable_from_GET[] = $_GET['variable_name'];

and then use the $variable_from_GET array variable in your conditions which you -I presume- you did since you had a $vars array.

David Thomas
+2  A: 

My take at it:

$vars = array("first_date_month", "first_date_day", "last_date_month", "last_date_day", "resume_date_month", "resume_date_day", "pay_date_month", "pay_date_day", "pay_time_hour", "pay_time_minutes");

foreach ($vars as $var) {
  if ($err_flag = empty($_GET[$var]))
    break;
}

8)

I assume the marked answer has... well.. answered the problems in your code, so just throwing out some optimizations:

  • using foreach() instead of while/for is simple in many cases where we are just iterating over an array - we can get the value and also the key if needed.
  • using the empty() function (returns true for anything null, false, 0, "", "0")
  • exit the loop when you're done using break
jcinacio
+1 Obviously even more compact. Why didn’t I think of `foreach`?
Gumbo