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 Object-Oriented PHP Basics Building the Recipe Static

Viktorija Marszalek
Viktorija Marszalek
2,142 Points

Problem with my code

Hey guys, i am trying to complete task in this chapter :

"The accepted parameter $size will be an array containing length and width in that order. Add a return value to the displayDimensions method that returns these values as a string "length x width". Example: 12 x 14"

I wrote this code, but the error occurs. Anyone knows how to improve it?

index.php
<?php

class Render {
  public $size = array('length'=> "", 'width'=> "");
  public static function displayDimensions($size){
    return $size{'length'} . " x " . $size{'width'};
  }
}
?>

12 Answers

Zeljko Porobija
Zeljko Porobija
11,491 Points
index.php
<?php
class Render {
  public $size = array('length'=> "", 'width'=> "");
  public static function displayDimensions($size){
    return $size[0] . " x " . $size[1];
  }
}
Zeljko Porobija
Zeljko Porobija
11,491 Points

There should be a sort of multidimensional array, yet this works well enough.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Given that they specifically told us the order of the length and width, I did my return statement like this. I tried Niels' answer, but couldn't make it work although it seems like it should!

return $size[0] . " x " . $size[1];

This is what worked for me :smiley:

gurpreet singh
gurpreet singh
9,625 Points

it works class Render { public $size = array('length'=> "12", 'width'=> "14"); public static function displayDimensions($size) { //$size = array('length'=> "5", 'width'=> "7"); return ("$size[0] x $size[1]"); } }

Niels Anders
Niels Anders
7,408 Points

I think it is : return $size['length'] . " x " . $size['width']; [] instead of {}

Niels Anders
Niels Anders
7,408 Points

Hmmmm.. I tested this code local on my computer and it works fine. i wil try that challange also soon. I let you know. Thx. How do you Highlight your code here?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You can right click the challenge link in the upper right hand corner here and choose open in a new tab. The code is done in markdown. There's a link at the bottom of this page for a markdown cheatsheet that explains it better than I would :thumbsup:

Viktorija Marszalek
Viktorija Marszalek
2,142 Points

thanks guys for your help! Unfortunately putting 'length' and 'width' in square brackets didn't work for me neither. But when I changed it to 0 and 1 it was accepted. No idea why.

Viktorija Marszalek
Viktorija Marszalek
2,142 Points

thanks guys for your help! Unfortunately putting 'length' and 'width' in square brackets didn't work for me neither. But when I changed it to 0 and 1 it was accepted. No idea why.

Richard Stiehm
PLUS
Richard Stiehm
Courses Plus Student 12,817 Points

Has anyone got this to pass...I've tried everything I could think of and all the suggestions here and none have worked. Although I've noticed that I've had a few things work locally that didn't in the challenge

Clare A
Clare A
23,994 Points

Hopefully you got an answer in the meantime, but just in case, this did the trick for me;

<?php

class Render {

  public $size = ["length"=> "", "width"=> ""];

  public static function displayDimensions($size)
  {
    return $size[0] . " x " . $size[1];
  }

}
  • In OP's example, curly braces were used in the return statement rather than square brackets
  • In one of the responses above, a new class has been instantiated but I didn't think this would be required as the method had been defined as static

Similar to the responders above, I wasn't able to pass the challenge when specifying the actual key names in the associative array ( i.e. "return $size['length'] . " x " . $size['width']" rather than return $size[0] . " x " . $size[1]; ) .....I'm not sure why this is but will post again if I find the answer.

jamesjones21
jamesjones21
9,260 Points

I know this is an old thread, but can someone please explain why it allows you to pass the challenge using:

because in my eyes we are naming the keys which then makes it an associative array, but yet we are passing it through as an indexed array ?

return $size[0] . " x " . $size[1];

and not

return $size['length'] . " x " . $size['width'];

thanks James

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi James Jones ! Yes, it's an old thread, but I have a little different perspective on it now than I did before. I believe the key lies in this line in Step 2:

The accepted parameter $size will be an array containing length and width in that order.

It says it's an array, not an associative array. Furthermore, had it been an associative array that was passed in, the order of the elements would not be relevant at all given that you can access them by their keys that way. But the order that they're sending in the elements is mentioned here, which means that it is just a regular array.

You can confirm this in the challenge by var_dump($size) and then using the preview button. Take a look what is output: array(2) { [0]=> int(12) [1]=> int(14) }.

Hope this helps! :sparkles:

jamesjones21
jamesjones21
9,260 Points

thanks, it's quite hard to understand where we've been taught to use => this when naming keys within arrays, but which makes it confusing.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I get it, but this is how any normal array will print out with var_dump. Take a look at this code (which might make the var_dump results more clear):

<?php
    $myArray = array("hi", "there", "John");
    var_dump($myArray);
?>

And this is the output of that var_dump:

array(3) { [0]=> string(2) "hi" [1]=> string(5) "there" [2]=> string(4) "John" }

The things you know as "indexes" work as keys. At index/key 0, is the string "hi". Basic arrays were covered in a course previous to this, I believe.

Niels Anders
Niels Anders
7,408 Points

Hi Jennifer. Good that you solved it. I tried this and it is working for me.

<?php

class Render {
public static function displayDimensions($size) { return $size['length'] . " x " . $size['width']; }

}

$Crender = new Render();

$size = array('length'=> "5", 'width'=> "7");

echo $render->displayDimensions($size);

?>

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I'm not the OP. But if I take your code and copy/paste it into the challenge it fails on step 2. And I don't know why. It seems like it should work in my opinion.