0

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++;  
}
3
  • 2
    Use an editor that highlights PHP’s syntax and bracket pairs. That will help you to avoid such errors. Commented Sep 27, 2009 at 21:34
  • 1
    You also shouldn't be using count() in the while loop. It'll have to re-count the elements every iteration. Save it to a var and use that. Commented Sep 27, 2009 at 21:38
  • also please use && and || instead of "and" and "or" they are more commenly used and make your code more readible. Commented Sep 27, 2009 at 21:47

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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") )

Comments

2

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

1 Comment

+1 Obviously even more compact. Why didn’t I think of foreach?
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.