Can this equation be done in php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
groog
Forum Newbie
Posts: 15
Joined: Wed Jun 18, 2008 11:48 am

Can this equation be done in php?

Post by groog »

Ok, so I have this equation and it keeps on turning up as 0.25 when it should be 1.85.

Code: Select all

($y-($x - 14)) * (0.05) + 0.3
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?
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Can this equation be done in php?

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can this equation be done in php?

Post by pickle »

You equation can simply be:

Code: Select all

($y - $x - 14) * 0.05 + 0.3
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
groog
Forum Newbie
Posts: 15
Joined: Wed Jun 18, 2008 11:48 am

Re: Can this equation be done in php?

Post 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-

Code: Select all

echo "The value for x is $x";
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?
Last edited by groog on Wed Jun 18, 2008 4:37 pm, edited 1 time in total.
ricky92
Forum Newbie
Posts: 3
Joined: Wed Jun 18, 2008 4:20 pm

Re: Can this equation be done in php?

Post 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;
}
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Can this equation be done in php?

Post 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.
groog
Forum Newbie
Posts: 15
Joined: Wed Jun 18, 2008 11:48 am

Re: Can this equation be done in php?

Post by groog »

It works! Thank you guys! Sorry I'm such a hassle :banghead:

PS.
Hey pickle, your "simpler" version

Code: Select all

($y - $x - 14) x 0.05 + 0.30
instead of

Code: Select all

($y - ($x - 14)) x 0.05 + 0.30
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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can this equation be done in php?

Post by pickle »

Something in PHP is buggered then, because those two equations are equivalent. Maybe PHP doesn't follow Order of Operations properly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
groog
Forum Newbie
Posts: 15
Joined: Wed Jun 18, 2008 11:48 am

Re: Can this equation be done in php?

Post 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 :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can this equation be done in php?

Post 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);
(#10850)
Post Reply