开发者

Should I use Hypens, Underscore or camelCase in php arrays?

开发者 https://www.devze.com 2022-12-21 12:38 出处:网络
I just started using php arrays (and php in general) I have a code like the following: languages.php: <?php

I just started using php arrays (and php in general)

I have a code like the following:

languages.php:

<?php
$lang = array(
    "tagline" => "I build websites...",
    "开发者_Go百科get-in-touch" => "Get in Touch!",
    "about-h2" => "About me"
    "about-hp" => "Hi! My name is..."
);
?>

index.php:

<div id="about">
   <h2><?php echo $lang['about-h2']; ?></h2>
   <p><?php echo $lang['about-p']; ?></p>
</div>

I'm using hypens (about-h2) but I'm not sure if this will cause me problems in the future. Any suggestions?


Between camel case and underscores it's personal taste. I'd recommend using whatever convention you use for regular variable names, so you're not left thinking "was this one underscores or camel case...?" and your successor isn't left thinking about all the ways they could torture you for mixing styles. Choose one and stick to it across the board.

That's why hyphens is a very bad idea - also, rarely, you'll want to use something like extract which takes an array and converts its members into regular variables:

$array = array("hello" => "hi", "what-up" => "yup");
extract($array);
echo $hello; // hi
echo $what-up; // FAIL

Personally, I prefer camel case, because it's fewer characters.


I'm actually surprised no one said this yet. I find it actually pretty bad that everyone brings up variable naming standards when we are talking about array keys, not variables.

Functionally wise, you will not have any problems using hyphens, underscores, camelCase in your array keys. You can even use spaces, new lines or null bytes if you want! It will not impact your code's functionality^.

Array keys are either int or string. When you use a string as a key, it is treated as any other string in your code.

That being said, if you are building a standardized data structure, you are better off using the same standard you are using for your variables names.


^ Unless you are planning to typecast to a (stdClass) or use extract(), in which case you should use keys which convert to valid variable names in order to avoid using ->{"My Key Is"} instead of ->myKeyIs. In which case, make sure your keys conform to [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.


You can use what ever feels most compfortable, however, I would not recommend using hypehens.

The common approach is to use camelCase and that is what a lot of standards in frameworks ask for.

I recommend using this.

 $thisIsMyVariable

 $this-is-not-how-i-would-do-it

Also, you can find that using hyphens could make your variables appear like subtracting. So you have to then use

 $object->{”Property-Here”}

Its just not nice. :(

Edit, sorry I just saw that you asked about the question in Array, not variables. Either way, my answer still applies.


I would append all into one word. If not appending it all together, I would either shorten it or use _.

In the long run, whatever you decide to choose just be consistent.


Many folks, including Zend, tell programmers to use camel case, but personally I used underscores as word separators for variable names, array keys, class names and function names. Also, all lowercase, except for class names, where I will use capitals.

For example:

class News
{
    private $title;
    private $summary;
    private $content;

    function get_title()
    {
        return $this->title;
    }
}
$news = new News;


The first result in Google for "php code standards" says:

  • use '_' as the word separator.
  • don't use '-' as the word separator

But, you can pretty much do whatever you want if you don't already have standards to follow. Just be consistent in what you do.


When looking at your code: Maybe you can even avoid some work with arrays and keys if you use something like gettext http://de2.php.net/manual/en/intro.gettext.php for your internationalization efforts from the very beginning.

This will finally result in

<h2><?php _("About me..."); ?></h2>

or

<?php 
$foo = _("About me..."); 
 ...
?>


I strongly recommend using underscore to separate words in a variable especially where the language is case sensitive. For non case sensitive languages like vb, camelCase or CamelCase is best.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号