why is this code returning 31?

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
southeastweb
Forum Newbie
Posts: 14
Joined: Sun Mar 21, 2004 3:28 am
Location: Florida

why is this code returning 31?

Post by southeastweb »

$variable50 = 3+7 * 4;

if anyone can answer this, i would be grateful. the above source is returning 31 on my echo, i know im missing something, cause normal logic says this = 40, thanks in advance! :(
aleigh
Forum Commoner
Posts: 26
Joined: Thu Mar 25, 2004 11:06 am
Location: Midwestern United States
Contact:

Re: why is this code returning 31?

Post by aleigh »

southeastweb wrote:$variable50 = 3+7 * 4;

if anyone can answer this, i would be grateful. the above source is returning 31 on my echo, i know im missing something, cause normal logic says this = 40, thanks in advance! :(
What you really mean is (3+7)*4. You can learn more at:

http://us4.php.net/manual/en/language.o ... precedence
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Remember BEDMAS

Brackets
Exponents
Division
Multiplication
Addition
Subtraction

That's the order.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

What??

Pemdas..?

Parenthesis
Exponents
Multiplication
Division
Addition
Subtraction
southeastweb
Forum Newbie
Posts: 14
Joined: Sun Mar 21, 2004 3:28 am
Location: Florida

yup

Post by southeastweb »

i knew it was something simple, never fails. thanks for the help, going to get my precedence straight! Frodo
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

I was always taught the phrase:

Please eat my delicious apple soup (Pemdas as punk stated)

it make no sense, apple soup? But I think thats why my class rememberd it, lol.

:wink:
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Please Excuse My Dear Aunt Sally. ;)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

like i said, I think the dumb completly made-up "apple soup" was the true trick behind the memory recall.

lol
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

PEMDAS FOR PRESIDENT!

:?
Image Image
southeastweb
Forum Newbie
Posts: 14
Joined: Sun Mar 21, 2004 3:28 am
Location: Florida

Exponent?

Post by southeastweb »

does exponent refer to the mathmatical term? does anyone know how to use this, or an example, im trying to figure it. thanks guys!


PEDMAS

People Eat Dollar Meals At Subway! :D
southeastweb
Forum Newbie
Posts: 14
Joined: Sun Mar 21, 2004 3:28 am
Location: Florida

my mistake not PEDMAS!

Post by southeastweb »

just to clear myself, i know what an exponent is, just wondering how you use it in php, and example? im heading to php.net thanks for PEMDAS
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

function i wrote:

Code: Select all

function raise($num, $exp){
    $value = 1;
    for($i=0;$i<$exp;$i++){
        $value = $value * $num;
    }
    return $value;
}
I'm sure PHP has a function though..
aleigh
Forum Commoner
Posts: 26
Joined: Thu Mar 25, 2004 11:06 am
Location: Midwestern United States
Contact:

Post by aleigh »

LiLpunkSkateR wrote:function i wrote:

Code: Select all

function raise($num, $exp){
    $value = 1;
    for($i=0;$i<$exp;$i++){
        $value = $value * $num;
    }
    return $value;
}
I'm sure PHP has a function though..
It does; pow()
Post Reply