views:

80

answers:

4
<?php
  class Student { public $name = "Benjamin"; }
  $name = new Student();
?>

<p>Hello, there. My name is <?php $name->name ?></p>

The above code doesn't work as intended (printing the name within "p" tags). But the below code, of course, does work:

<?php
class Student { public $name = "Benjamin"; }
$name = new Student();

echo '<p>Hello, there. My name is ' . $name->name . '</p>';
?>

Is the class being destructed when closing PHP tags?

Is there a work-around for the second code example?


Thanks, as always.

+6  A: 

Your forgot to echo $name->name, so your code should look like:

<p>Hello, there. My name is <?php echo $name->name ?></p>
Residuum
+4  A: 

Don't forget the echo in

<p>Hello, there. My name is <?php echo $name->name ?></p>

Is the class being destructed when closing PHP tags?

No

chelmertz
+1  A: 

You're missing an echo before $name, so it becomes:

<?php echo $name->name; ?>

livingtech
A: 

I haven't touched PHP in a long time but,

  • no it doesn't
  • would <?php $name->name ?> really print it?
Novikov