sting to integer conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhanrajesh210002
    New Member
    • Aug 2010
    • 6

    sting to integer conversion

    hi,

    I'm struck in string conversion into integer,

    if my $string1 is "(10+20)"
    1. how to calculate the value of the string,
    2. how to convert this string and string value into integer

    and generally how to convert string intointeger...

    kindly update asap.
    thanks
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    In order to get the value of a statement in a string, you could make use of the eval() function. Just be sure that you validate all input to ensure that no malicious code is executed.

    As for the general conversion of strings to integers and vice versa, just use typecasting. PHP is a very loosely typed language, so variable types are flexible.

    i.e.
    Code:
    $value = "10"; // currently a string
    $value = (int)$value; // now, it is an integer
    $value = (string)$value; // now, it is a string again
    Last edited by Dormilich; Sep 6 '10, 05:46 AM.

    Comment

    Working...