SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pakistan
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question I am saving operators(+,-,*) in mysql table have type varchar i have problem

    I am saving operators(+,-,*) in mysql table have type varchar i have problem when in puting the values in formul e.g

    $val1=7;
    $val2=+;
    $val3=5;
    $val4=-;
    $val5=9;

    $ans=$val1.$val2.$val3.$val4.$val5;

    the output is:

    7+5-9

    but i want to perform the action and give me the result 3

    how to do?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,146
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    I can't think of another way to do that besides forming a valid PHP assignment as a string and using eval().
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pakistan
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks oddz

    its not working ........giving parse error

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    what do you want to do with said value?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by oddz View Post
    I can't think of another way to do that besides forming a valid PHP assignment as a string and using eval().
    After you have verified that the input consists of digits, operators and parentheses only, of course
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Weird, I tried this but it didn't work. Either I'm missing something very basic here, or... well, I must be something really basic

    PHP Code:
    <?php

    $val0 
    '$res=';
    $val1 7;
    $val2 '+';
    $val3 5;
    $val4 '-';
    $val5 9;
    $val6 ';';

    $sum $val0 $val1 $val2 $val3 $val4 $val5 $val6;

    echo 
    $sum "<br />";
    eval(
    '<?php ' $sum ' ?>');

    var_dump(isset($res));

    ?>

    Output:

    Code:
    $res=7+5-9;
    
    boolean false

    p.s. I have, of course, checked that the input consists of digits, operators and parentheses only

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    You don't put <?php and ?> in the eval, just the code you want to evaluate.

    PHP Code:
    <?php
    $val0 
    '$res=';
    $val1 7;
    $val2 '+';
    $val3 5;
    $val4 '-';
    $val5 9;
    $val6 ';';

    $sum $val0 $val1 $val2 $val3 $val4 $val5 $val6;

    echo 
    $sum "<br />";
    eval(
    $sum);

    var_dump(isset($res));
    var_dump($res);

    /*
    $res=7+5-9;
    boolean true
    int 3
    */
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    p.s. I have, of course, checked that the input consists of digits, operators and parentheses only
    if the values are coming from user input you might have to do more checks, i.e.

    PHP Code:
    $val0 '$res=';
    $val1 '/'// user supplied
    $val2 '/'// user supplied
    $val3 ';'
    eval() now throws a parse error
    aaron-fisher.com - PHP articles and more

  9. #9
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You don't put <?php and ?> in the eval, just the code you want to evaluate.
    Of course, that just goes to show how often I use eval().


Bookmarks

Posting Permissions

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