2. Types and Operators
Activity 2.1: Printing the BMI of a User
Solution
- Create a new file called
tracker.php
. Then, open PHP and create a variable to store the name. You can assign a value directly, in other words,$name = 'Joe'
:<?php $name = 'Joe';
- Add variables for the weight and height; again, set a default value:
$weightKg = 80; $heightCm = 180;
- Take the
$heightCm
variable, convert it to meters by dividing by100
, and then store the result:$heightMeters = $heightCm/100;
- Square the height and store the result:
$heightSquared = $heightMeters * $heightMeters;
- Calculate the BMI by taking the weight and dividing it by the squared height:
$bmi = $weightKg / ($heightSquared);
- Display a message to the user showing the name and BMI result:
echo "<p>Hello $name, your BMI is $bmi</p>";
- Open the Terminal/Command Prompt and navigate to your
chapter2
folder or where you storedtracker.php
. Run the server by typing in...