w3resource

PHP class for student information


7. Student Class

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.

For more Practice: Solve these Related Problems:

  • Write a PHP class to calculate and display a student's grade average from multiple test scores stored in an array.
  • Write a PHP script that extends the Student class to include methods for updating and retrieving extracurricular activity data.
  • Write a PHP program to sort a list of Student objects based on grade, then display the top student’s details.
  • Write a PHP function to validate student data before instantiating the Student class, ensuring no empty names or invalid age values.

Go to:


PREV : Library System Class Hierarchy.
NEXT : BankAccount Class.

PHP Code Editor:



Contribute your code and comments through Disqus.

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.