w3resource

PHP Class Exercises : Display a string

PHP class: Exercise-1 with Solution

Write a simple PHP class which displays the following string.

'MyClass class has initialized !'

Sample Solution:

PHP Code:

<?php
class MyClass {
  public function __construct() 
  {
   echo 'MyClass class has initialized !'."\n";
  }
 }
$userclass = new MyClass;
?>

Sample Output:

MyClass class has initialized !

Flowchart :

Flowchart: Display a string

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: PHP Classes Exercises Home.
Next: Write a simple PHP class which displays an introductory message like "Hello All, I am Scott", where "Scott" is an argument value of the method within the class.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.