This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by jabbiati
almost 10 years

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();
    ?>

Answer 5378624f9c4e9de9f20035af

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!

points
Submitted by Gaurang Tandon
almost 10 years

2 comments

Islam Sabr about 9 years

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

Sumanth over 8 years

same

Answer 53be98198c1ccc7e64001c01

5 votes

Permalink

points
Submitted by Young
almost 10 years

4 comments

Bilszima over 8 years

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

Aleksadnar Maodus over 8 years

same code as me :D

NguyenHieuEC over 8 years

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

Darkhorse2 over 8 years

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

Answer 549691109113cbecc9004ac6

4 votes

Permalink

100 percent Works!

points
Submitted by Nouman Ashraf Awan
over 9 years

Answer 551719e351b8874f69002075

3 votes

Permalink

points
Submitted by Lloyd Lopes
about 9 years

Answer 53980d538c1ccc7bc9001340

1 vote

Permalink

just remove the

and

in your return meow meow

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

reason is your already in

tag

points
Submitted by Geoffrey Ayap
almost 10 years

Answer 5423405152f863b989000337

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 T_VARIABLE, expecting T_FUNCTION 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();
points
Submitted by tomaokazaki
over 9 years

2 comments

Gaurang Tandon over 9 years

Your class curly bracket class Dog { is not closed.

tomaokazaki over 9 years

thx (:

Answer 537774d552f863b48e00152d

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?

points
Submitted by jabbiati
almost 10 years

Answer 53777637631fe9ef9d001715

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?”

points
Submitted by jabbiati
almost 10 years

1 comments

Tim Bouscal over 9 years

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.

Answer 53dcda019c4e9dd38b001a23

0 votes

Permalink

// it work 100%

points
Submitted by chand1992
over 9 years

2 comments

mustardnose over 9 years

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

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

echo $cat1->meow();

Answer 556820d19113cb960b0003fe

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

points
Submitted by str111
almost 9 years

Answer 55ab35ae76b8fed6bc0002fa

0 votes

Permalink

whats wrong with this 6/10

points
Submitted by Sumanth
over 8 years

Answer 55d227609113cb4b150002d5

0 votes

Permalink

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

points
Submitted by Abhaya10
over 8 years

Answer 5615466ee39efe7768000230

0 votes

Permalink

Thanks for your help guys!

points
over 8 years

Answer 55e49f1cd3292fea8c00062d

-1 votes

Permalink

points
over 8 years

Answer 53bdbda180ff3300ae0009ab

-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();
        
    ?>
points
Submitted by Azrael.1987
almost 10 years

2 comments

Azrael.1987 almost 10 years

It’s work.

Young almost 10 years

Answer 5395e64f80ff332d45000540

-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.

points
Submitted by Zuo Bruno Foy
almost 10 years

2 comments

mikedegrande almost 10 years

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

Zuo Bruno Foy almost 10 years

@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.