473,785 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple calculations in PHP

Cowbie
5 New Member
Hello everyone,

I'm very new to PHP and I'm keen to learn how it all works. I've been looking for help all day and reading tutorials etc which is how I came accross this forum - so here's hoping for some answers :)

First off I'll tell you what I'm doing. I'm making a calculator of some form which will do some simple calculations on the numbers entered into a form. For example, adding, multiplying etc. At the moment just to get me going I want to add two numbers together which would have been entered by the user. The calculator itself will be used to estimate the top speed of a car depending on what's been entered (doesn't have to be accurate of course, it's by no means professional or anything like that it's just college work).

I may have made some silly mistakes because of how new all this is to me, but here is my code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Form :: Results</title>
  4. </head>
  5.  
  6. <body>
  7. <h1>Top Speed Calulator</h1>
  8. <img src="gtx.jpg" alt="My VW Scirocco GTX">
  9. <br>
  10. <br>
  11. Here are the options you submitted:
  12. <br>
  13. <br>
  14.  
  15. <table border="3" bgcolor=#FFFF99>
  16. <tr>
  17.  <th>Property</th>
  18.  <th>Value</th>
  19. </tr>
  20.  
  21. <tr>
  22.  <td>Chassis Weight (lbs)</td>
  23.  <td><?php echo number_format($_POST['chassis']); ?></td>
  24. </tr>
  25.  
  26. <tr>
  27.  <td>Brake Horse Power (BHP)</td>
  28.  <td><?php echo number_format($_POST['bhp']); ?></td>
  29. </tr>
  30.  
  31. <tr>
  32.  <td>Number of Doors (3\5)</td>
  33.  <td><?php echo htmlspecialchars($_POST['doors']); ?></td>
  34. </tr>
  35.  
  36. <tr>
  37.  <td>Dropped Suspension? Lowered by... (mm)</td>
  38.  <td><?php echo htmlspecialchars($_POST['drop']); ?></td>
  39. </tr>
  40.  
  41. <tr>
  42.  <td>Engine Modifications</td>
  43.  <td><?php echo htmlspecialchars($_POST['mods']); ?></td>
  44. </tr>
  45.  
  46. <tr>
  47.  <td>Fuel Feed Type</td>
  48.  <td><?php echo htmlspecialchars($_POST['fuelfeed']); ?></td>
  49. </tr>
  50.  
  51. <tr>
  52.  <td>Your email address (for our newsletter)</td>
  53.  <td><?php echo htmlspecialchars($_POST['email']); ?></td>
  54. </tr>
  55. </table>
  56.  
  57. <?php $speed = $chassis + $bhp;?>
  58. <?php echo number_format($speed); ?>
  59.  
  60. <br>
  61. </body>
  62. </html>
  63.  
As you can probably see, right now it only shows you what the user has entered on the form on the previous page, but the code at the bottom is where I'm trying to get the calculations to work - right now adding the chassis weight and the bhp values together.

Can anyone shed some light on this for me?

Many thanks

PS - I can give you the code for the form itself if that's of any help to anyone
Nov 5 '07 #1
5 2188
Motoma
3,237 Recognized Expert Specialist
You will first need to parse through your expression and transform it into a Parse Tree or RPN . Afterwards, analyze the elements and operators and perform the appropriate operations on the running total.
Nov 5 '07 #2
Cowbie
5 New Member
Thanks for those links. I think I didn't quite explain myself properly in my original post. The problem I *think* I'm having is the code I've written:

<?php $speed = $chassis + $bhp;?>
<?php echo number_format($ speed); ?>

It always seems to return 0 rather than any other number. Why is this? Can anyone tinker with my code or hand me some pointers?
You can look at the actual website here

I apologise for not having a good knowledge of all this and if those links will help me a great deal more than they have so far, I'm just rather confused right now which is why I'm posting up here. Usually, I don't post on forums regarding issues like this as I can mostly sort them using tutorials elsewhere - but this has me around the bend!
Nov 5 '07 #3
Motoma
3,237 Recognized Expert Specialist
Thanks for those links. I think I didn't quite explain myself properly in my original post. The problem I *think* I'm having is the code I've written:

<?php $speed = $chassis + $bhp;?>
<?php echo number_format($ speed); ?>

It always seems to return 0 rather than any other number. Why is this? Can anyone tinker with my code or hand me some pointers?
You can look at the actual website here
Okay, I understand your problem a little better now.
Your issue is that you are using $chassis and $bhp without ever assigning values to them.
Nov 5 '07 #4
Cowbie
5 New Member
Okay, I understand your problem a little better now.
Your issue is that you are using $chassis and $bhp without ever assigning values to them.
Ok, that helps me a great deal, thanks!
However, I thought it would have taken the values from the ones shown in the table - for example $_POST['chassis']

How can I fix this, or assign them the values given in the table?


EDIT:
Ok, I think I have it figured thanks to your post. I did:

<?php $chassis = $_POST['chassis'] ?>
<?php $bhp = $_POST['bhp'] ?>

<?php $speed = $chassis + $bhp;?>
<?php echo number_format($ speed); ?>

It now seems to be working fine. Great thanks!
However, I'm wondering if this was this the correct way to fix the problem?
Nov 5 '07 #5
Motoma
3,237 Recognized Expert Specialist
Ok, that helps me a great deal, thanks!
However, I thought it would have taken the values from the ones shown in the table - for example $_POST['chassis']

How can I fix this, or assign them the values given in the table?


EDIT:
Ok, I think I have it figured thanks to your post. I did:

<?php $chassis = $_POST['chassis'] ?>
<?php $bhp = $_POST['bhp'] ?>

<?php $speed = $chassis + $bhp;?>
<?php echo number_format($ speed); ?>

It now seems to be working fine. Great thanks!
However, I'm wondering if this was this the correct way to fix the problem?

Yes, this was a perfectly acceptable way to solve the problem, though elegant it is not.

A nicer way would be:

[php]
<?php
$speed = $_POST['chassis'] + $_POST['bhp'];
echo number_format($ speed);
?>
[/php]

or even

[php]
<?php
echo number_format($ _POST['chassis'] + $_POST['bhp']);
?>
[/php]
Nov 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
1673
by: Mathieu Malaterre | last post by:
Hi, I tried a simple addition with python and I don't understand what is going on: $python >>464.73+279.78 744.50999999999999 weird isn't it ? I use python2.3.
11
4678
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
3
2578
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age groupings. The calculation is: unemployment rate = number of unemployed/number of labour force
5
2236
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its nitty gritty but its necessary for my program. thanks dave
14
6575
by: Xarky | last post by:
Hi, I would like to enrypt and decrypt a simple line of text, with a private(symmetric) key. I have tried searching in the System.Cryptography class, but I can't find a simple way of doing this job. Can someone recommend me an easy way on how to do it. Thanks in Advance
18
1926
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point operations and disregard all integer operations. My question is this. I am writing a simulation for animal dispersement through large landscapes. We are loading data from several different files to simulate the environment the animals will be...
4
1858
by: SijSaru13 | last post by:
Dear everybody, I have to write a program that implements a real-time multicast calculator service. Users join a multicast group and are able to send equations to other clients for calculation - no separate server is needed. Calculations are sent in a single string to the multicast group and the result is returned once it has been calculated by a client in single string containing the equation and its result. The client must be able to...
8
1533
by: giddy | last post by:
Hi , i made this simple little app that displays all .NET colours and handles resize and mouse events. But if one tries to resize the app , it mucks up and performs disgustingly slow. Heres the source: http://gidsfiles.googlepages.com/ColorBox.cs
9
2629
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of the main floors of the main house: Part C: Gross Floor Area of the basement or cellar: Part D: Gross Floor Area of the attic:
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.