Do I need to declare a PHP array before adding values ​​using []?

$arr = array(); // is this line needed? $arr[] = 5; 

I know this works without the first line, but is often used in practice.

What is the reason? Is it safe without him?

I know you can do this too:

  $arr = array(5); 

but I'm talking about cases where you need to add items one at a time.

+72
arrays php
Nov 23 2018-11-11T00:
source share
11 answers

If you do not declare a new array, and the data that creates / updates the array is not suitable for any reason, then any future code that tries to use the array will be E_FATAL because the array does not exist.

For example, foreach() will throw an error if the array has not been declared and no values ​​have been added to it. However, no errors will occur if the array is simply empty, as if you would have declared it.

+85
Nov 23 '11 at 16:58
source share

I just want to note that the PHP documentation on arrays really talks about this in the documentation.

From a PHP site with an accompanying code snippet:

 $arr[key] = value; $arr[] = value; // key may be an integer or string // value may be any value of any type 

"If $arr does not exist yet, it will be created, so this is also an alternative way to create an array."

But, as the other answers said ... you really have to declare a value for your variables, because all kinds of bad things can happen if you don't.

+23
Nov 23 '11 at 17:06
source share

Php is a freely typed language. This is perfectly acceptable. At the same time, real programmers always declare their vars.

+13
Nov 23 '11 at 17:00
source share

Think about the coders that come after you! If you just see $arr[] = 5 , you don’t know what $arr can be without reading all the previous code in the area. The explicit string $arr = array() makes it clear.

+6
Nov 23 '11 at 17:10
source share

it's just a good practice. Let's say you added to your array inside a loop (a fairly common practice), but then accessed the array outside the specified loop. Without an array declaration, your code will cause errors if you never hit the loop.

+4
Nov 23 '11 at 16:57
source share

I highly recommend declaring an array before adding values. In addition to all of the above, if the array is inside a loop, you can inadvertently insert elements into your array. I just noticed that this creates a costly mistake.

 //Example code foreach ($mailboxes as $mailbox){ //loop creating email list to get foreach ($emails as $email){ $arr[] = $email; } //loop to get emails foreach ($arr as $email){ //oops now we're getting other peoples emails //in other mailboxes because we didn't initialize the array } } 
+3
Dec 06 '17 at 12:21
source share

Without declaring the array before use, it can really cause problems. One experience that I just found, I called this test script as follows: indextest.php? File = 1STLSPGTGUS This works as expected.

 //indextest.php?file=1STLSPGTGUS $path['templates'] = './mytemplates/'; $file['template'] = 'myindex.tpl.php'; $file['otherthing'] = 'otherthing'; $file['iamempty'] = ''; print ("path['templates'] = " . $path['templates'] . "<br>"); print ("file['template'] = " . $file['template'] . "<br>"); print ("file['otherthing'] = " . $file['otherthing'] . "<br>"); print ("file['iamempty'] = " . $file['iamempty'] . "<br>"); print ("file['file'] = " . $file['file'] . "<br>");// should give: "Notice: Undefined index: file" print ("file = " . $file);// should give: "Notice: Undefined index: file" //the Output is: /* path['templates'] = ./mytemplates/ file['template'] = myindex.tpl.php file['otherthing'] = otherthing file['iamempty'] = Notice: Undefined index: file in D:\Server\Apache24\htdocs\DeliverText\indextest.php on line 14 file['file'] = Notice: Array to string conversion in D:\Server\Apache24\htdocs\DeliverText\indextest.php on line 15 file = Array */ 

Now I just need a file from another script I bought at the top, and we will see how the values ​​are completely incorrect for the $ file array, while the $ path array is fine: "checkgroup.php" is to blame.

 //indextest.php?file=1STLSPGTGUS require_once($_SERVER['DOCUMENT_ROOT']."/IniConfig.php"); $access = "PUBLIC"; require_once(CONFPATH . "include_secure/checkgroup.php"); $path['templates'] = './mytemplates/'; $file['template'] = 'myindex.tpl.php'; $file['otherthing'] = 'otherthing.php'; $file['iamempty'] = ''; print ("path['templates'] = " . $path['templates'] . "<br>"); print ("file['template'] = " . $file['template'] . "<br>"); print ("file['otherthing'] = " . $file['otherthing'] . "<br>"); print ("file['iamempty'] = " . $file['iamempty'] . "<br>"); print ("file['file'] = " . $file['file'] . "<br>"); print ("file = " . $file); //the Output is: /* path['templates'] = ./mytemplates/ file['template'] = o file['otherthing'] = o file['iamempty'] = o file['file'] = o file = oSTLSPGTGUS */ 

Initializing an array earlier, then no problem!

 //indextest.php?file=1STLSPGTGUS require_once($_SERVER['DOCUMENT_ROOT']."/IniConfig.php"); $access = "PUBLIC"; require_once(CONFPATH . "include_secure/checkgroup.php"); $path = array(); $file = array(); $path['templates'] = './mytemplates/'; $file['template'] = 'myindex.tpl.php'; $file['otherthing'] = 'otherthing.php'; $file['iamempty'] = ''; print ("path['templates'] = " . $path['templates'] . "<br>"); print ("file['template'] = " . $file['template'] . "<br>"); print ("file['otherthing'] = " . $file['otherthing'] . "<br>"); print ("file['iamempty'] = " . $file['iamempty'] . "<br>"); print ("file['file'] = " . $file['file'] . "<br>"); print ("file = " . $file); //the Output is: /* path['templates'] = ./mytemplates/ file['template'] = myindex.tpl.php file['otherthing'] = otherthing.php file['iamempty'] = file['file'] = file = Array */ 

That is why I realized how important it is to initialize the variables, since we never know what problem we may encounter later, and just to save time we could end up spending even more. I hope that it will be useful for people like me who are not professional.

+1
Nov 12 '18 at 0:39
source share

It depends on your error checking. If you have an error message according to strict rules, you will receive a notification, but it will still work without it.

0
Nov 23 '11 at 16:57
source share

Well, if you need it as a global variable or if you want to reuse the same array over and over again in different functions

0
Nov 23 '11 at 16:59
source share

This is your code

 $var[] = 2; print_r($var) 

Works great! until someone declares the same variable name in front of your charming code

 $var = 3; $var[] = 2; print_r($var) 

Warning: it is not possible to use a scalar value as an array

Unfortunately!

This is one possible (sometimes unpredictable) case, so yes, $var = array() is required

 $var = 3; $var = array(); $var[] = 2; print_r($var) 

Exit

 Array ( [0] => 2 ) 
0
Dec 10 '18 at 16:45
source share

Agree with @djdy, there is only one option I would like to post:

 <?php // Passed array is empty, so we'll never have $items variable available. foreach (array() AS $item) $items[] = $item; isset($items) OR $items = array(); // Declare $items variable if it doesn't exist ?> 
-one
Nov 23 '11 at 17:06
source share



All Articles