Re321 Posted April 9, 2010 Share Posted April 9, 2010 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 https://forums.phpfreaks.com/topic/198076-parse-error-syntax-error-unexpected-t_if-expecting-t_function/ Share on other sites More sharing options...
Zyx Posted April 9, 2010 Share Posted April 9, 2010 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 https://forums.phpfreaks.com/topic/198076-parse-error-syntax-error-unexpected-t_if-expecting-t_function/#findComment-1039322 Share on other sites More sharing options...
Re321 Posted April 9, 2010 Author Share Posted April 9, 2010 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 https://forums.phpfreaks.com/topic/198076-parse-error-syntax-error-unexpected-t_if-expecting-t_function/#findComment-1039324 Share on other sites More sharing options...
Re321 Posted April 9, 2010 Author Share Posted April 9, 2010 Help I want to add to the array, so I can plug it into http_build_query or whatever to send to curl. Link to comment https://forums.phpfreaks.com/topic/198076-parse-error-syntax-error-unexpected-t_if-expecting-t_function/#findComment-1039355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.