Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAaron Boyse
Courses Plus Student 1,924 PointsConditionals in PHP
I have this code here:
[code] <?php $studentOneName = 'Dave'; $studentOneGPA = 3.8;
$studentTwoName = 'Treasure'; $studentTwoGPA = 4.0;
//Place your code below this comment
?> [/code]
I am trying to create a if/else statement in PHP to check if the student has a GPA of 4.0 or not.
My question is, do I need to create a third student variable to check this?
1 Answer
Ezra Siton
12,644 PointsFirst Markdown your code : https://teamtreehouse.com/library/markdown-basics
In your case, boolean statements should work fine (No need for extra var - but it's better to store GPA = 4.0 in a variable).
- https://teamtreehouse.com/library/booleans
- https://stackoverflow.com/questions/1670138/php-if-statement-for-boolean-values-var-true-vs-var
Example
NOTE: This code is very minimal - In the future, you can add loops throw array of students, output messages for each student, create functions, and so on - and get more modular code - follow treehouse tracks)
<?php
$GPA = 4.0;
$studentOneName = 'Dave';
$studentOneGPA = 3.8;
$studentTwoName = 'Treasure';
$studentTwoGPA = 4.0;
if($GPA === $studentTwoGPA){
echo 'Well done';
}
else{
echo 'keep learning';
}