0

Please bare with my I'm used in C++. I want to use an array in PHP (with variable size) but it must be filled with zeroes uppon declaration. I know that I shouldn't "declare" variables and arrays in PHP but I need it filled before use.

$counter[] = {0}; Sends out an "unexpected '{' in line 53" error although this would perfectly work in C++. I had wrote that code in another server and it worked perfectly (in PHP) but it stoped working when I moved the files. What can I use for similar results?

2
  • 3
    C++ !== PHP - Perhaps consider using array_fill() to create it? Commented Sep 19, 2017 at 16:26
  • 1
    I don't believe {0} has ever been valid syntax in PHP - just 0 should do the job. Or use array_fill, as @MarkBaker says. Commented Sep 19, 2017 at 16:27

1 Answer 1

1

You can use array_fill - http://php.net/manual/en/function.array-fill.php

EG:

$counter = array_fill(0,5,0);

5 being the amount of 0's you want.

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

1 Comment

The problem with array_fill is that you need to declare an ammount of "0's" you want. I need a variable size array that by default has the value 0 in it. Of course it might be the case with PHP that every array is filled with 0's and not NULL's like C++ but I'm not sure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.