Page 1 of 1
Can this equation be done in php?
Posted: Wed Jun 18, 2008 3:26 pm
by groog
Ok, so I have this equation and it keeps on turning up as 0.25 when it should be 1.85.
Substitute 32 for y and 15 for x. Get a calculator and try this out, what do you get? Then make a php file and echo this out (as a number, not a string). What do you get?
Can you have double parenthesis in php?
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 3:54 pm
by nowaydown1
I just did some testing on this in various PHP environments. Here's the test script that I used to verify this:
Code: Select all
<?php
$y = 32;
$x = 15;
echo ($y-($x - 14)) * (0.05) + 0.3
?>
I got 1.85 on every configuration that I tested:
-PHP 4.4.4
-PHP 4.4.8
-PHP 5.0.4 (Both Win32 & Linux 2.4.27)
-PHP 5.2.6
What version of PHP are you running? What platform are you on?
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 4:03 pm
by pickle
You equation can simply be:
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 4:30 pm
by groog
*EDIT AT THE BOTTOM*
But I'm using XAMPP which a lot of php people recommend and use on youtube so I'm guessing it's all up to date.
Ok, so I've found that when this equation is simpley echoed out it's fine. But one thing I didn't mention that this is a custom function. So try this code and see what happens-
Code: Select all
<html>
<body>
<?php
$y = 32;
$x = 15;
function test()
{
echo (32-(15 - 14)) * (0.05) + 0.3;
}
function test2()
{
echo ($y-($x - 14)) * (0.05) + 0.3;
}
test();
echo '<br />';
test2();
?>
</body>
</html>
test() turns out to be 1.85 and test2() turns out 1. What do you guys get. And the value for $y comes from the previous page and the value for $x comes from a separate php file (using the require function to call it).
Ok so here is my php info-
PHP Version 5.2.5
System Windows NT MICAH-2OWX70V5B 5.0 build 2195
Build Date Nov 8 2007 23:18:08
Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared"
Server API Apache 2.0 Handler
Virtual Directory Support enabled
Configuration File (php.ini) Path C:\WINNT
Loaded Configuration File C:\x\apache\bin\php.ini
PHP API 20041225
PHP Extension 20060613
Zend Extension 220060519
Debug Build no
Thread Safety enabled
Zend Memory Manager enabled
IPv6 Support enabled
Registered PHP Streams php, file, data, http, ftp, compress.zlib, zip
Registered Stream Socket Transports tcp, udp
Registered Stream Filters convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, zlib.*
Edit:
Ok, hold up. The $x when transfered can be put like this and work-
And 32 will show up. But it seems when put into the function the $x has the value of 0. How can I fix this?
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 4:36 pm
by ricky92
groog wrote:But I'm using XAMPP which a lot of php people recommend and use on youtube so I'm guessing it's all up to date.
Ok, so I've found that when this equation is simpley echoed out it's fine. But one thing I didn't mention that this is a custom function. So try this code and see what happens-
Code: Select all
<html>
<body>
<?php
$y = 32;
$x = 15;
function test()
{
echo (32-(15 - 14)) * (0.05) + 0.3;
}
function test2()
{
echo ($y-($x - 14)) * (0.05) + 0.3;
}
test();
echo '<br />';
test2();
?>
</body>
</html>
test() turns out to be 1.85 and test2() turns out 1. What do you guys get. And the value for $y comes from the previous page and the value for $x comes from a separate php file (using the require function to call it).
Ok so here is my php info-
PHP Version 5.2.5
System Windows NT MICAH-2OWX70V5B 5.0 build 2195
Build Date Nov 8 2007 23:18:08
Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared"
Server API Apache 2.0 Handler
Virtual Directory Support enabled
Configuration File (php.ini) Path C:\WINNT
Loaded Configuration File C:\x\apache\bin\php.ini
PHP API 20041225
PHP Extension 20060613
Zend Extension 220060519
Debug Build no
Thread Safety enabled
Zend Memory Manager enabled
IPv6 Support enabled
Registered PHP Streams php, file, data, http, ftp, compress.zlib, zip
Registered Stream Socket Transports tcp, udp
Registered Stream Filters convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, zlib.*
You refer to $x and $y in a function; unless you declare $x and $y globals, it won't work (the parser will give them a 0 value). Just replace your test2 function with this:
Code: Select all
function test2()
{
global $x, $y;
echo ($y-($x - 14)) * (0.05) + 0.3;
}
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 4:39 pm
by nowaydown1
Exactly what ricky said. $x and $y aren't in the current function scope. So either pass them as parameters or do the global thing.
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 5:16 pm
by groog
It works! Thank you guys! Sorry I'm such a hassle
PS.
Hey pickle, your "simpler" version
instead of
Coincidentally worked for that number, but when I changed the value of $y (which will change) it didn't equal the correct answer. So thanks for the new headache xD
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 5:38 pm
by pickle
Something in PHP is buggered then, because those two equations are equivalent. Maybe PHP doesn't follow Order of Operations properly.
Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 5:54 pm
by groog
Nah, cause I tried the order of operations and it worked just fine. I just picked the one number that would coincidentally not need parenthesis around the $x - 14. I just thought it was kinda funny

Re: Can this equation be done in php?
Posted: Wed Jun 18, 2008 6:05 pm
by Christopher
I would recommend
not using global. This would be better:
Code: Select all
$y = 32;
$x = 15;
function test2($x, $y)
{
return ($y-($x - 14)) * (0.05) + 0.3;
}
echo test2($x, $y);