EchoFool Posted May 25, 2010 Share Posted May 25, 2010 Hey, I am trying to return my function without assigning it to a variable so i can use it in an if statement but it seems to be syntax'd incorrectly - what i am trying to do is this: <?php function catexists($RecordID){ $Get = mysql_query("SELECT Count(RecordID) AS ID,StaffPost,StaffView FROM catergories WHERE RecordID='".SQLskip($RecordID)."'") or die(mysql_error()); $row = mysql_fetch_assoc($Get); return array($row['ID'],$row['StaffPost'],$row['StaffView']); } If(isset($_GET['cat']) && catexists($_GET['cat'])[0] == 1){ //display position 0 of array here } ?> But the error i get is : Parse error: syntax error, unexpected '[' in on line 18 Yet it is in an array so i don't see why it won't work, but i cant assign a variable for the if statement either. Unless i can do it "in" the if statememnt? Link to comment https://forums.phpfreaks.com/topic/202884-return-function-as-array/ Share on other sites More sharing options...
kenrbnsn Posted May 25, 2010 Share Posted May 25, 2010 I don't believe you can do this in PHP and have to use a variable: <?php if (isset($_GET['cat'])) { $tmp = catexists($_GET['cat']); if ($tmp[0] == 1) { // // etc... // } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/202884-return-function-as-array/#findComment-1063217 Share on other sites More sharing options...
EchoFool Posted May 25, 2010 Author Share Posted May 25, 2010 Damn thats a shame but ill have to use two IF's then lets hope future PHP versions support it Link to comment https://forums.phpfreaks.com/topic/202884-return-function-as-array/#findComment-1063219 Share on other sites More sharing options...
PFMaBiSmAd Posted May 25, 2010 Share Posted May 25, 2010 Given that your function executes a query and returns more than one value and that you are then going to want to access those other values inside the conditional logic, you likely would not want it to work the way you tried as you would then be calling the function multiple times. That would not be very kind to your database server, i.e. executing the query each time you referenced any element of catexists($_GET['cat'])[x]. Link to comment https://forums.phpfreaks.com/topic/202884-return-function-as-array/#findComment-1063229 Share on other sites More sharing options...
EchoFool Posted May 25, 2010 Author Share Posted May 25, 2010 Thats a good point Didn't think of that. Link to comment https://forums.phpfreaks.com/topic/202884-return-function-as-array/#findComment-1063233 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.