This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Objects in PHP Forum

View Course

1194 points
E92b6f9531f17ce12b50d4c50fbcd9eb?s=140&d=retro
Submitted by
jabbiati
over 3 years ago

8/10 and 9/10 <p></p> don't contain anything

I am getting an error message saying that my tags are empty even though the result pane is showing the proper echo. I have tried resetting the code and refreshing my browser page and I still get the same error message. Here is my code in case I am doing something wrong.

<?php
      // Your code here
      class Cat {
          public $isAlive = true;
          public $numLegs = 4;
          public $name;
          public function __construct($name){
              $this->name = $name;
          }

          public function meow() {
              return "<p>Meow meow</p>";
          }
      }

      $cat1 = new Cat("CodeCat");
      echo $cat1->meow();
    ?>

10 votes

Best answer

permalink

When the PHP code is like this:

<p>
    <?php
        echo "Personal Homepage: Hypertext Processor";
    ?>
</p>

Whatever is echoed comes directly inside of the <p> tags. So, the above code would, on executing, produce:

<p>Personal Homepage: Hypertext Processor</p>

To test, apply a CSS rule, say color:blue; on p element, and notice the text that PHP echoed is blue!

Now, note that the <?php ?> tags for this exercise are already inside of p tags. So, whatever you echo, say "Meow meow!", would come directly inside of the p tag, and the exercise would pass you.
But by adding an extra <p>, and outputting like echo "<p>Meow meow</p>", it would confuse the code checker and it would throw an error.

Thus, simply, don't use the <p> tags in output strings.

Hope that clarifies your doubt!

2570 points
56ec11ba1ec4b00bc400069e_656519138
Submitted by
Gaurang Tandon
over 3 years ago

2 Comments

55061fff9113cbcaab007221_388739080 Islam Sabr over 2 years ago

This is crazy my code was Perfect but not working I have copy pasted the same code from here it is working. I have learnt nothing, I don't know where was the mistake

55beeddd937676b2de000046_535113983 Sumanth about 2 years ago

same


5 votes

permalink

<?php
class Cat {
public $isAlive = true;
public $numLegs = 4;
public $name;
public function __construct($name){
$this->name = $name;
}
public function meow(){
return "Meow meow";
}
}
$cat1 = new Cat("CodeCat");
echo $cat1->meow();
?>

216 points
948a9ffd51f3551b92f0fc0785ab7c4e?s=140&d=retro
Submitted by
Young
about 3 years ago

4 Comments

0c693cdcd29943e9e8df755856494927?s=140&d=retro Bilszima about 2 years ago

you have the exact same code I had and for some reason yours works and mine didn't. what the actual fu. how very frustrating

Picture Aleksadnar Maodus about 2 years ago

same code as me :D

Picture NguyenHieuEC about 2 years ago

i copy my code then refresh the page and paste it back it work :)

563f564eb62548d5c9000854_286301216 Darkhorse2 almost 2 years ago

That's what I do too, half the time. It's ridiculous.


4 votes

permalink

100 percent Works!

<?php

   class Cat {
    public $isAlive = true;
    public $numLegs = 4;
    public $name;

    function __construct($name) {
    $this->name = $name;
    }
    public function meow(){
    return "Meow meow";
    }
    }
    $name = new Cat("CodeCat");
    echo $name->meow();
    ?>

381 points
551704e086f552f0ef001e15_757977277
Submitted by
Nouman Ashraf Awan
almost 3 years ago


3 votes

permalink

<?php
class Dog {
public $numLegs = 4;
public $name;

public function __construct($name){
$this->name = $name;
}
public function bark() {
return "Woof!";
}
public function greet() {
return "Hello! my name is ". $this->name ." And I am a Dog";
}
}

$dog1 =new Dog("Barker");
$dog2 = new Dog("Amigo");

echo $dog1->bark(). "";
echo $dog2->greet();
?>

492 points
Picture
Submitted by
Lloyd Lopes
over 2 years ago


1 vote

permalink

just remove the and in your return meow meow

public function meow() {
        return "Meow meow";
}

reason is your already in tag

427 points
552fa41d86f55226e7000061_720625731
Submitted by
Geoffrey Ayap
over 3 years ago


1 vote

permalink

I would like to know why this is not working?
i pass the question but i get this error:
Parse error: syntax error, unexpected TVARIABLE, expecting TFUNCTION on line 24
class Dog{
public $numLegs = 4;
public $name;

            public function __construct($name){
                $this->name = $name;
            }
            public function bark(){
                return "Woof!";
            }
            public function greet(){
                return "Hi my name is " . $this->name . " i am awesome.";
            }

            $dog1 = new Dog("Barker");
            $dog2 = new Dog("Amigo");

            echo $dog1->bark();
            echo "<br />";
            echo $dog2->greet();

1286 points
1323cc3acadfd5fdf1db9fc851935655?s=140&d=retro
Submitted by
tomaokazaki
almost 3 years ago

2 Comments

56ec11ba1ec4b00bc400069e_656519138 Gaurang Tandon almost 3 years ago

Your class curly bracket class Dog { is not closed.

1323cc3acadfd5fdf1db9fc851935655?s=140&d=retro tomaokazaki almost 3 years ago

thx (:


0 votes

permalink

Not really adding an answer to my own question, but rather adding more of my confusion. I removed the tags from the meow() method and now the code passes. I thought that the tags were supposed to be there. Other lessons worked with the tags in. Don't these tags allow multiple echos to display on separate lines rather than in a row on the same line?

1194 points
E92b6f9531f17ce12b50d4c50fbcd9eb?s=140&d=retro
Submitted by
jabbiati
over 3 years ago


0 votes

permalink

Got the same error on 10/10 with this code:

 <?php /*Your code here */ 
  echo "<p>Now I know the basics of OOP!</p>";
  ?>

Again, removed the tags and it passed. I guess I am just too over zealous with the paragraph tags. So... my question now is "When should tags be used in a function's return and when should they not be used?"

1194 points
E92b6f9531f17ce12b50d4c50fbcd9eb?s=140&d=retro
Submitted by
jabbiati
over 3 years ago

1 Comment

52122d52f10c60ada0002320_407409648 Tim Bouscal over 2 years ago

the issue isn't your tags. The issue is that the confirmation tests are VERY specific. If your final HTML doesn't look the way it expects you will fail.


0 votes

permalink

// it work 100%

<?php

  class Cat{
      public $isAlive = true;
      public $numLegs = 4;
      public $name;

      public function __construct($name){
          $this->name = $name;
      }
      public function meow(){
            return "Meow meow";
      }
  }
      $cat1 = new Cat("CodeCat");
      echo $cat1->cat();
    ?>

343 points
53dd2e6b548c35ee230003b8_756405289
Submitted by
chand1992
about 3 years ago

2 Comments

69e5f23fb7bbe15785bd864e8606d6fa?s=140&d=retro mustardnose almost 3 years ago

you need to change the call to the method:
echo $cat1 -> cat();
to
echo $cat1 -> meow();

531b79587c82cad45e002559_414186014 Валентин Георгиев almost 3 years ago

Correct syntax is : $cat1 = new Cat("CodeCat");

echo $cat1->meow();


0 votes

permalink

class cat{
public $alive=true;
public $numlegs=4;
public $name;
public function __construct($name){
$this->name=$name;
}
public function meow(){
return "meow meow";
}
}

          $name =new cat("codecat");

          echo $name->meow(); 

``but it say did u create a cat class ???
what to do

111 points
1f96b188244043de81be32d891b99e5e?s=140&d=retro
Submitted by
str111
over 2 years ago


0 votes

permalink

whats wrong with this 6/10

<!DOCTYPE html>


Reconstructing the Person Class




<?php
class person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)"

$teacher = new peerson("tired", "54321", 54321);
$student = new person("Sumanth", "Maddirala", 20);
echo $teacher->greet();
echo $student->greet();
}
}

    ?>
  </p>
</body>

526 points
55beeddd937676b2de000046_535113983
Submitted by
Sumanth
about 2 years ago


0 votes

permalink

Don't use an instance called $cat1 , instead use $name as given in the instructions.

172 points
5b20afb15c933b6b4e466b06b76524b3?s=140&d=retro
Submitted by
Abhaya10
about 2 years ago


0 votes

permalink

Thanks for your help guys!

173 points
5394fc257c82ca3f79004e0f_893261855
Submitted by
Marcus Vinicius Cruz Silva
almost 2 years ago


-1 votes

permalink

<!DOCTYPE html>


Practice makes perfect!




<!-- Your code here -->

    <?php
    class Dog {
        public $numLegs = 4;
        public $name;
        public function __construct($name); {

            $this -> name = $name;
            }

        public bark() {
            return "Woof!";
            };
     public greet(){
         return "My Dog name is" ."$this ->name .";
         }
        }

        $dog1 = new Barker($name);
         $dog2 = new Amigo($name);

         echo $dog1 -> bark();
          echo $dog2 -> Amigo();
    ?>
  </p>
</body>

501 points
55dc829a86f552a56a0001f2_892706734
Submitted by
Akosile Samuel Ilemobayo
about 2 years ago


-3 votes

permalink

    <?php
      class Cat{
          public $isAlive = true;
          public $numLegs = 4;
          public $name;

          public function __construct($name){
              $this->name = $name;
          }
          public function meow(){
                return "Meow meow";
          }
      }
          $cat1 = new Cat("CodeCat");
          echo $cat1->meow();

    ?>

300 points
8176d0a9d675b403638039f2fe8f20d9?s=140&d=retro
Submitted by
Azrael.1987
about 3 years ago

2 Comments

8176d0a9d675b403638039f2fe8f20d9?s=140&d=retro Azrael.1987 about 3 years ago

It's work.

948a9ffd51f3551b92f0fc0785ab7c4e?s=140&d=retro Young about 3 years ago

<?php
class Cat {
public $isAlive = true;
public $numLegs =4;
public $name;
public function __construct($name){
$this->name = $name;
}
public function meow(){
return "Meow meow";
}
}
$cat1 = new Cat("CodeCat");
echo $cat1->meow();
?>

what's wrong with mine?


-5 votes

permalink

I think your code at this level:

 $cat1 = new Cat("CodeCat");

Is wrong. The $cat1 is not defined to which property it belongs in the Cat class. It should look like this:

   $cat1 = new Cat($name = "CodeCat");

This sends the new "CodeCat" to the name property.

1402 points
5392255f9c4e9dcced000a4f_596925879
Submitted by
Zuo Bruno Foy
over 3 years ago

2 Comments

Daa5ae6319865c23f58e9de921601389?s=140&d=retro mikedegrande over 3 years ago

Wouldn't your constructor direct it to the proper property?

5392255f9c4e9dcced000a4f_596925879 Zuo Bruno Foy over 3 years ago

@mikedegrande its good you try my suggestion, if it works learn from what I said, if not please post error in your reply. And NO. Computers do not "Think" they do as they are told and in this case you have not explained what it should do properly. So the computer is a bit confused.