Syntax PHP error expecting T_FUNCTION getting random

I have the code below that is suppose to randomly pick a different Google api upon getting a video request from the Youtube api. My script gets video data and puts the videos on a Wordpress site. The code below is giving me this error.

syntax error, unexpected '$apiKeys' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)

Here is my code.

$apiKeys = array(
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000');

$rand = rand(0,4);  

private $google_api_key = $apiKeys[$rand];

Can someone help me figure out why I’m getting this error? Thank you.

What is causing that error is above what you posted. That error is caused when something isn’t closed properly and says the error is below the actual cause.

This is everything above that code.

<?php

final class Video_Krakken
{
$apiKeys = array(
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000');

$rand = rand(0,6);  

private $google_api_key = $apiKeys[$rand];

You are in a class. You need to give access modifiers to the variable.

Ok, thank you for your advice. Please excuse my ignorance. I’m not a great programmer. I’m still learning as I go. What do you mean by give access modifiers to the variable?

<?php

final class Video_Krakken
{
    private $apiKeys = array(
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000',
                  'AIzaSyDLszH3Dlpiu2ANEbL0SNZmiKJoArZ0000');

    $rand = rand(0,6);  
    private $google_api_key = $this->apiKeys[$rand];
    public function get_key() {
        return $this->google_api_key;
    }

}
Sponsor our Newsletter | Privacy Policy | Terms of Service