phpstringvolumemultiplicationcubic

PHP multiplication string


I have just begun learning PHP, and I have been working on a practice page for finding the cubic volume of a cube. The information is taken from submissions on an html form and I have written the code for taken the submitted information as the variables but I am not entirely sure how to set up the result.

here is my code so far

<?php
  #Script calculates cubic volume 

  $length=$_POST["length"];

  $width=$_POST["width"];

  $height=$_POST["height"];

  $result=

I am just not sure how to format the $result line so that it will properly multiply the length, width, and height. I also apologize for such a beginner question, I just couldn't find the answer at other sources.


Solution

  • As the values coming from the POST array are all strings, it's better to manually cast them to ints (or floats, if need be) instead of relying on PHP's automatic type casting.

    $result = (int)$length * (int)$width * (int)$height;  
    print $result;