Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am trying to build an indexed array of array's in php, not a key value array of arrays, I keep getting parse error. Can you show me where my mistake is? Why is it that I can set the values of the singly dimensioned arrays but not $ax? Thanks!

<?php
class mdArray{
public $a0 = array('10','20','30','40','50','60','70','80','90');
public $a1 = array('11','21','31','41','51','61','71','81','91');
public $a2 = array('12','22','32','42','52','62','72','82','92');
public $a3 = array('13','23','33','43','53','63','73','83','93');
public $a4 = array('14','24','34','44','54','64','74','84','94');
public $a5 = array('15','25','35','45','55','65','75','85','95');
public $ax = array($a0,$a1,$a2,$a3,$a4,$a5);
}
?>
share|improve this question

marked as duplicate by Tim Cooper May 23 at 19:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Which line has a syntax error? What is the exact error you get? –  Rocket Hazmat May 23 at 19:13
1  
@RocketHazmat - I'd imagine public $ax = array($a0,$a1,$a2,$a3,$a4,$a5); which is dependent on run-time information –  Mark Baker May 23 at 19:16
2  
Why don't you do : class mdArray{ public $ax = array( array('10','20','30','40','50','60','70','80','90'), array('11','21','31','41','51','61','71','81','91'), array('12','22','32','42','52','62','72','82','92'), array('13','23','33','43','53','63','73','83','93'), array('14','24','34','44','54','64','74','84','94'), array('15','25','35','45','55','65','75','85','95'), ); } –  Mark Baker May 23 at 19:17
    
@RocketHazmat -Parse error: parse error, expecting ')'' in - on line 9` –  demuro1 May 23 at 19:23
    
@MarkBaker I am writing a function that takes small values to create a string with implode. I add a single charter to the end of the string, I do this for every array, then I implode all the strings together into one string, I'm trying to pass only one parameter into the function –  demuro1 May 23 at 19:31

1 Answer 1

It's probably this line:

public $ax = array($a0,$a1,$a2,$a3,$a4,$a5);

You need to do that in your constructor:

<?php
    class mdArray{
        public $a0 = array('10','20','30','40','50','60','70','80','90');
        public $a1 = array('11','21','31','41','51','61','71','81','91');
        public $a2 = array('12','22','32','42','52','62','72','82','92');
        public $a3 = array('13','23','33','43','53','63','73','83','93');
        public $a4 = array('14','24','34','44','54','64','74','84','94');
        public $a5 = array('15','25','35','45','55','65','75','85','95');
        public $ax;

        function __construct(){
            $this->ax = array($this->a0,$this->a1,$this->a2,$this->a3,$this->a4,$this->a5);
        }
    }
?>

DOCS: http://www.php.net/manual/en/language.oop5.properties.php

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.