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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php-exercises/oop/php-oop-exercise-7.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics