Jump to content

Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION ??


Re321

Recommended Posts

Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION

 

I get this error on a simple scraping class i am trying to make:

class automate {

  var $targeturl = '';
  var $ua = Array('Firefox'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
  var $cookiefile = 'cookies.txt';
  var $getvars = Array();
  var $postvars = Array();
  var $opt = "";
  
  function post($var, $val) {
    $postvars[] = Array($var=>$val); 
  }
  
  function get($var, $val) {
    $getvars[] = Array($var=>$val); 
  }

  if($opt == "GET") {
    $q = $getvars;
  }elseif($opt == 'POST') {
    $q = $postvars;
  }

  if(!empty($post) || !empty($get)) {
    $params = http_build_query($q);
  }

}

 

I get the line on the if statement, I don't see what is wrong!? I tried commenting out those functions before it (cause i dunno if they work or not) but it still gives me the error no matter what .

 

why can't i use if statement in a class? o_O

Take a look, where you close the curly bracket for the class - close it right after the methods, because classes must not contain ordinary statements placed directly within their body.

 

Anyway, there is another bug in you code - how do we access the object fields and how do you access to them? What PHP tutorial/book are you using?

 

PS. If you are going to use classes, at least don't use such archaic syntax, because PHP 4 is no longer developed and PHP 5 style is "a bit" different.

Take a look, where you close the curly bracket for the class - close it right after the methods, because classes must not contain ordinary statements placed directly within their body.

 

Anyway, there is another bug in you code - how do we access the object fields and how do you access to them? What PHP tutorial/book are you using?

 

PS. If you are going to use classes, at least don't use such archaic syntax, because PHP 4 is no longer developed and PHP 5 style is "a bit" different.

 

I put all of it in a function and referred to them thru $this->q and $this->opt, I forgot about doing that, thanks!

 

but my problem now is this:

function post($var, $val) {
    $postvars[] = Array($var=>$val); 
  }

 

would doing $obj->post('name', 'abc') work??

I dunno how to add an value with key to an array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.