PDA

View Full Version : Pow calculation and newer versions of PHP


Gil
08-13-2002, 01:38 PM
Hi!

The form below is working on my local server but not online where it produces the following error:

Invalid argument(s) passed to pow() in /etc/etc.

If I would first give $aaa $bbb and $ccc a value, the error dessapears but the pow calculation uses the variables I made up instead of the one submitted by the form.

CCC should be 1+choise1^3 = 3^3 = 3x3x3=27

<form action="rekentest.php">
<select name="choise1">
<option value="2">2
</select>

<select name="choise2">
<option value="3">3
</select>

<p><input type="submit">

<?php
$aaa= (1 + $choise1);
$bbb = pow($aaa,$choise2);
$ccc = round($aaa* $bbb);
echo "<p>$ccc";
?>
</form>


Offline there's no problem.

Any suggestions?

Gil

mordred
08-13-2002, 02:24 PM
The second last usernote on the manual page of pow() describes what went wrong: http://se.php.net/manual/en/function.pow.php

Hint: Form variables do always appear as strings. It's a type-missmatch problem.

Gil
08-13-2002, 02:42 PM
Hi and Thanks,

I'm reading, and trying. The page you refered to is where I learned how to use pow. It's still not very clear to me though. I can read english but to understand it just like that is harder.

Can you show me how to do it the right way?

Thanks,

Gil

mordred
08-13-2002, 06:27 PM
Sure I can, I hope you understand my english better than the manual notes, because I'm not a native speaker. ;)

When you work with variables that were generated by the form variables submitted, each of them is a string variable. What strings are and what pecularities should be known can be read from the manual.
Your problem is now that by definition, the pow() function expects two arguments that must be numbers, i.e integer or float types. So this

echo pow(2, 4); // echoes 16

echo pow(2, "4"); // throws error

you get a warning with the second version. It's because "4" is a string and does not get converted to an integer automatically, this has to be done explicitly by you like this

echo pow(2, intval("4")); // ok now

So in your case,

$bbb = pow($aaa, intval($HTTP_GET_VARS['choise2']));

should do the trick.
Note: Why did I use this $HTTP_ thingy? Because in newer PHP versions, form variables are not automatically extracted to the global namespace as ordinary variables. You have to access them through thes $HTTP arrays or through the superglobals explained here:
http://se.php.net/manual/en/language.variables.predefined.php

Gil
08-13-2002, 08:23 PM
Thanks a lot!!!!!!!!

I'm trying to understand but I't's working for now.

I'm gonna read it all soon...

Gil

pager
08-14-2002, 03:18 AM
Geez Morded,
If you're not a native speaker, could you come here to the usa and start teaching the natives how to speak then?

Your english and writing skills are absolutely superb.

Keep posting, your help is appreciated.