Is it nice to use spaces in PHP associative array indexes?

I need to pass some parameters using an associative array, something like this:

$blockGroup['name=products type=complete'] 

While doing some tests, I saw that it works, but is this a bad practice? Is it possible to generate any error or unexpected behavior?

Thanks for any suggestion!

EDIT 1

I use this array in the implementation of the view, full structure:

 $blockGroup['name=products type=complete'][] = array( 'name' => 'GeForce', 'value' => '99.99' ); 
+4
source share
6 answers

No no. The symbol of space in programming does not really matter much. Characters enclosed in quotation marks and forming a string can be used as keys of associative arrays.

In fact, there are many times when using such keys for associative arrays will make your code accessible and convenient for making changes to it.

 $scores = array("John Doe" => 100, "Ivan Ivanovich" => 75.3); 

I see that you are trying to use array keys as an expression, which is REALLY bad practice. Things are for what they are for. Use associative keys as associative keys.

+5
source

It will work and not a bad practice . Whitespace is just a regular char in the index line. No problem with that.

As in many programming situations, the indexes you use in the array are dynamically created, this is necessary. Indexes can even be binary strings. Check out this example, a typical situation. We want to remove duplicate lines from the file and print each line only once:

file.txt

 hello world. foo bar hello world 123 

example.php

 $printed = array(); foreach(file('file.txt') as $line) { if(isset($printed[$line])) { continue; // skip the line } echo $line; $printed[$line] = true; // line will contain spaces } 
+2
source

Answering your original question, related arrays in PHP map keys with values. In this case, the keys you use are strings, which in this case all PHP takes care is not necessarily the contents of these strings (i.e. they may or may not have spaces). I say this, since you do not seem to be sure whether it is legal in that language (and not just whether it was bad practice).

As for bad practice, this is not the one that I know of. It depends on the context, if this kind of key card naturally matches the value you want to keep, then it should be perfect.

+1
source

I would say yes, this is bad practice, but it will work. Everything you try to achieve can be done differently. Can I suggest a multidimensional array? Or maybe a key and a meaning?

I suppose something like this can cause headaches in the future. More space for IMO spelling mistakes. Plus it's not very nice to watch.

Keep in mind that I specifically refer to your example. Something in these lines is fine:

 array('toaster oven' => 100, 'heater' => 50); 
+1
source

This is valid syntax, but it may be the best way to achieve what you are trying to do.

Arrays can have integer, FLOating, Null, Bool and String keys.

In the case of strings, PHP represents them internally as a sequence of bytes, so as far as I know, there is no way to inject vulnerabilities using spaces or special characters for your keys.

However, in my opinion, it makes the code less readable and more error prone by incorrectly typing the key and spending countless hours to find out what you should have entered

$myKey['name=guitar price=200.00']

instead:

$myKey['name=gutiar price=200.00']

the PHP manual page gives a detailed explanation of why this is a bad method:

At some point in the future, the PHP team may want to add another constant or keyword, or a constant in other code may interfere. For example, it is already wrong to use the words empty and default in this way, since they are reserved for keywords.

http://php.net/manual/en/language.types.array.php

+1
source

I don't think spaces are a problem in keys, but I think using equal characters seems inconvenient. Based on your code example, I don't understand why you simply would not use 3 dimensions in your array like this:

 $blockGroup['products']['complete'][] = array( 'name' => 'GeForce', 'value' => '99.99' ); 

Perhaps I misunderstand your situation, but for me it seems more logical.

+1
source

All Articles