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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.