Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

James MacDonald
PLUS
James MacDonald
Courses Plus Student 6,268 Points

Array syntax in PHP 5.2.13?

Hi, I'm following along on a server running PHP 5.2.13, and it looks like the lesson is using 5.4+ array syntax. Is there an easy way to "convert" the lesson code to this crusty old version? I tried many times to get the arrays to work correctly with no luck. any help is appreciated. Happy to provide code samples if that would help.

3 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

As of PHP 5.4 there is new shorthand array syntax.

Pre PHP 5.4:

<?php

/* Initialize new empty array */

$array = array();


/* Associative Array */

$array = array(
    "key" => "value",
    "key" => "value"
);


/* Non-Associative Array */

$array = array("value", "value");


/* Associative Array Push */

array_push($array, "key", "value");


/* Non-Associative Array Push */

array_push($array, "value");

?>

PHP 5.4+ :

<?php

/* Initialize new empty array */

$array = [];


/* Associative Array */

$array = [
    "key" => "value",
    "key" => "value"
];


/* Non-Associative Array */

$array = ["value", "value"];


/* Associative Array Push */

$array["key"] = "value";  /* Will change value rather than push if key exists already */


/* Non-Associative Array Push */

$array[] = "value"; 

?>

Hope this helps :)

James MacDonald
PLUS
James MacDonald
Courses Plus Student 6,268 Points

Not sure what I was doing wrong earlier, but I figured this out. Square brackets (aside from the array index) need to be converted to array().

So this:

<?php

$catalog[101] = [
    "title" => "Design Patterns",
    "authors" => [
        "Erich Gamma",
        "Richard Helm"
    ],
    "publisher" => "Prentice Hall"
];

?>

becomes this:

<?php

$catalog[101] = array(
    "title" => "Design Patterns",
    "authors" => array(
        "Erich Gamma",
        "Richard Helm"
        ),
    "publisher" => "Prentice Hall"
    );

?>

Sorry - can't figure out how to indent code in this editor...

Codin - Codesmite
Codin - Codesmite
8,600 Points

I edited your post so you can see how to post php to the forum (if you go to edit your post you can see how I have done it (it is hard to explain in a reply as if I type what to do it turns into a codeblock).

So basically it is 3 tildes ( ` ) then the code type (in the case php, you can change it to ruby, html, css, javascript, python etc to change the colour linting).

Then at the end of your code 3 more tildes ( ` ) to close the code block.

For PHP to get colour linting you have to use php opening and closing tags around your code

<?php 

?>

Also leave a space between your text/code and both sets of 3 tildes ( ` ), I find if you do not have a space between them it can have weird results.

James MacDonald
PLUS
James MacDonald
Courses Plus Student 6,268 Points

Had to look it up, but this: (`) is called a "backtick", whereas (~) is the tilde. So much learning here... Thanks again.