Find the answer to your Linux question:
Results 1 to 5 of 5
I've just started to learn php and i noticed that i cannot perform calculations with php. See example. <?php $number1 = 10; $result += 2; echo $number1; ?> according to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    47

    problem with mathematical calculations in php


    I've just started to learn php and i noticed that i cannot perform calculations
    with php. See example.

    <?php
    $number1 = 10;

    $result += 2;

    echo $number1;
    ?>

    according to a video tutorial i am watching the result should be 8
    echoing out 8 but it doesnt. Is there a setting in php.ini for blocking this ?
    One that i might have accidentally sometime turned off.

    Note my error_handling is turned off.

    I am able to perform something like this. see below without any problem

    <?php
    // Ex4 if else statements with more comparisons.

    $number = 112;
    if ($number==10) {
    echo 'equal to ten.';
    } else if ($number==11){
    echo 'Equal to eleven.';
    } else {
    echo 'Not equal.';
    }
    ?>


    Thanks in advance guys...

  2. #2
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,315
    Lets have a look at the code and add some comments
    Code:
    <?php
    
    //  Set $number1 to the value of 10
    $number1 = 10;
    
    //  Try and add 2 to the as yet undefined $result (will produce warning)
    $result += 2;
    
    //  Output $number1 which is still equal to 10
    echo $number1;
    ?>
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    Conkybots: Interactive plugins for your Conkys!

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,174
    1. $result is never set, just incremented by 2.
    2. $number1 is never changed from the original value (10).

    So, if you want to increment $number1 by 2, after setting it to 10, and output that, then do this:
    Code:
    $number1 = 10;
    $number1 += 2;
    echo $number1;
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Nov 2012
    Posts
    5
    Hello all,

    Since this is the only thread that I could find regarding math calculations I will post here.

    The problem that I have is connected with bc. I need to apply an equation and troubles come when bc meets 10^(90/249.3)..

    The input data has 6 columns (for now), I've put them into arrays.

    For this particular case the variables are constants, (further I need to use values from chart), the calculated n0 will be appended to the input file.

    This is the input (the only value that I use for now is from the last column - 'ng' ) :

    01.08.2012,00:02,744.286,20.4,71,1.0002958
    01.08.2012,00:07,744.286,20.4,71,1.0002958
    01.08.2012,00:12,744.361,20.3,71,1.0002958




    Here is an example of what i've done:


    #!/bin/bash

    exec 4<ng.txt

    IFS=','

    while read -a AREPLY -u 4
    do

    p=${AREPLY[2]}
    t=${AREPLY[3]}
    r=${AREPLY[4]}
    ng=${AREPLY[5]}
    n0=${AREPLY[6]}

    echo ${AREPLY[0]},${AREPLY[1]},${p},${t},${r},${ng},${n0}


    t=12
    p=760
    a=7.5
    b=237.3
    r=60

    n0=$(bc << EOF
    scale = 7
    c = ($ng-1)/(1+0.003661*$t)
    d = ($p/760)
    e = (5.5*10 ^ -8 )/(1+0.003661*$t)
    f = 4.58
    g = 10^($a*$t/$b+$t)
    h = ($r/100)
    1 + c * d - e * f * g * h
    EOF
    )


    echo > n0
    done

    exec 4<&-

    exit 0




    And this is what I get:

    "Runtime warning (func=(main), adr=28 ): non-zero scale in exponent"

    Any advice on this is highly appreciated!

    Thanks,
    Ralu

  5. #5
    Just Joined!
    Join Date
    Nov 2012
    Posts
    5
    The problem is solved, awk did the job!

    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •