Jump to content

Return Function as array?


EchoFool

Recommended Posts

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

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].

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.