w3resource

PHP class for student information

PHP OOP: Exercise-7 with Solution

Write a PHP class called 'Student' with properties like 'name', 'age', and 'grade'. Implement a method to display student information.

Sample Solution:

PHP Code :

<?php
class Student {
    public $name;
    public $age;
    public $grade;

    public function displayInfo() {
        echo "Name: " . $this->name . "</br>";
        echo "Age: " . $this->age . "</br>";
        echo "Grade: " . $this->grade . "</br>";
    }
}

$student = new Student();
$student->name = "Gwladus Andrea";
$student->age = 16;
$student->grade = 10;
$student->displayInfo();
?>

Sample Output:

Name: Gwladus Andrea
Age: 16
Grade: 10

Explanation:

In the above exercise -

  • The "Student" class has three public properties: $name, $age, and $grade, which represent the student's name, age, and grade.
  • The "displayInfo()" method is implemented to display student information by echoing the values of the properties.

Flowchart:

Flowchart: PHP class for student information.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: PHP class hierarchy for library system.
Next: Deposit and withdraw.

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.