Forum Moderators: coopster

Message Too Old, No Replies

Help with php maths

Syntax for php maths calculations

         

ed_aus

12:24 pm on Aug 13, 2006 (gmt 0)

10+ Year Member



Hi, I'm new to php. I got stuck on syntax to perform some calculations on server side. In particular, I'm trying to calculate a log and round the number to 0 decimal places, here are the details but I can't get it to work:

<? php
$q = 360/$_POST['thenumber'];

$z= round(float log(float$q [, 2]));

?>

$_POST['thenumber'] is a number from a form passed to a current page
I need to divide 360 by 'thenumber'

returned $q variable to be used in logarthmic calculation - base 2
$z variable to be the whole number only (to be used in another part of the script)

Any assistance much appreciated.

jatar_k

3:47 pm on Aug 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld ed_aus,

looks like a syntax issue

$q = 360/$_POST['thenumber'];
$z= round(float log(float$q [, 2]));

you shouldn't need to typecast those numbers. I also imagine you are copying the syntax from the manual. You don't need those floats. They are just telling you what type is expected or returned. Also the square brackets mean that it is an optional param in the function call.

It should look something like this

$q = 360/$_POST['thenumber'];
$z= round(log($q,2));

try that and see what happens

supermoi

3:53 pm on Aug 13, 2006 (gmt 0)

10+ Year Member



That should be:

$q = 360/$_POST['thenumber'];

$z= round(log($q,2));

When you go to php.net and look at function definitions, there's a few things you have to know ;). The "float" that stood before the variable's name means that the variable must be of the type float. (See [php.net...] for more infos about types) You must not actually write this when you call a function.
Same for the brackets. They mean that the parameter inside them is optional. You have to write this parameter when you call the function only if you want its value to be different than the default one. Once, again, the brackets should not remain there.
Hope it makes things a little clearer.

ed_aus

9:56 am on Aug 14, 2006 (gmt 0)

10+ Year Member



Thanks for your prompt response and warm welcome! Much appreciate.

Of course, it worked. Eazy and simple! I'd better read more about the basics so I don't make that mistake twice :-O

Cheers

"The beginner"