0

I just don't understand why I get this message:

Parse error: syntax error, unexpected '[', expecting ')' on line 11

At this file:

    <?php 
class Telegram
{   
    protected $notice = array();
    private $db;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function AddNew($token,$cat,$ads[$i],$key[$j])
    {
        if(!empty($token)&&!empty($cat)&&!empty($ads))
        {
            $new = $this->con->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, ".$ads[$i].", ".$key[$i].")");
            $new->bindParam(1,$token);
            $new->bindParam(2,$cat);
            $new->bindParam(3,$ads);
            $new->bindParam(4,$key);
            $new->execute();
            $notice['success_message'] = "New Telegram Channel was successfully added";
            return $this->notice;

        }
    }
    public function getNotice()
    {
        return $this->notice;
    }
}
?>

And line 11 is this:

public function AddNew($token,$cat,$ads[$i],$key[$j])
5
  • 2
    Because the syntax is invalid. What are you trying to achieve, it doesnt make much sense Commented Sep 25, 2017 at 21:41
  • You can see my other question to understand the theory behind this: stackoverflow.com/questions/46404664/… Commented Sep 25, 2017 at 21:44
  • The value should just be passed to the variable. Commented Sep 25, 2017 at 21:46
  • Someone might pass in a value to like $ads[3], but you can't grab as a parameter $ads[3], you just get the value $ads Commented Sep 25, 2017 at 21:46
  • If you followed the advice of either of the answerers in your original question, you wouldn't even be having this problem. This is a perfect example of an XY problem. Note that the very first comment here states "doesn't make much sense" -- that's a great indicator. Commented Sep 25, 2017 at 21:49

3 Answers 3

2

You can't use array's index in functions parameters.

public function AddNew($token,$cat,$ads,$key)
{

}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to just pass the array in parameter (and the $i variable separately if you want to access to specific data in your array) :)

1 Comment

Change your prototype like that: public function AddNew($token, $cat, $ads, $i, $key, $j)
0

Try this you can pass Array and key like this

public function AddNew($token,$cat,$ads,$key)
{
    if(!empty($token)&&!empty($cat)&&!empty($ads))
    {
        $new = $this->con->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, ".$ads[$i].", ".$key[$i].")");
        $new->bindParam(1,$token);
        $new->bindParam(2,$cat);
        $new->bindParam(3,$ads);
        $new->bindParam(4,$key);
        $new->execute();
        $notice['success_message'] = "New Telegram Channel was successfully added";
        return $this->notice;

    }
}

2 Comments

I can't do that, because at the other page I'm calling this: $tel = new Telegram($token, $cat, $ads[$i], $key[$j]); $notice[] = $tel->AddNew();
@dasoudeabiat You need to pass the value in the AddNew(param, param, param, param) so like in your function AddNew($token,$cat,$ads,$key) so in your example just put at $tel->AddNew($token,$cat,$ads[$i],$key[$j]); here you can.