add up an integer with the result from substr()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MuffinMan

    add up an integer with the result from substr()

    Hi,

    I'm trying to add up an integer with the result from substr() like

    $second_int = substr( .... ) + $int;

    I know the result from substr() is an integer and so is $int but in the end
    $second_int only has the result from the substr() and hasn't add up the
    $int. What am I doing wrong in here... tried quite a few things but well....

    Thanks in advance, Maarten


  • Pedro Graca

    #2
    Re: add up an integer with the result from substr()

    MuffinMan wrote:[color=blue]
    > I'm trying to add up an integer with the result from substr() like
    >
    > $second_int = substr( .... ) + $int;[/color]

    Lookss good.
    [color=blue]
    > I know the result from substr() is an integer and so is $int but in the end
    > $second_int only has the result from the substr() and hasn't add up the
    > $int. What am I doing wrong in here...[/color]

    No idea ...
    [color=blue]
    > tried quite a few things but well....[/color]

    why don't you post what you tried?




    $ cat xx.php
    <?php
    $int = 17;
    $text = '01234';

    $y = array();
    $y[] = substr($text, 1, 2) + $int;
    $y[] = substr($text, 1) + $int;
    $y[] = substr($text, 1, 55) + $int;
    $y[] = substr($text, 1, -2) + $int;
    $y[] = substr($text, 1, -22) + $int;
    $y[] = substr($text, -3, 2) + $int;
    $y[] = substr($text, -3) + $int;
    $y[] = substr($text, -3, 55) + $int;
    $y[] = substr($text, -3, -2) + $int;
    $y[] = substr($text, -3, -22) + $int;

    print_r($y);
    ?>

    $ php xx.php
    Array
    (
    [0] => 29
    [1] => 1251
    [2] => 1251
    [3] => 29
    [4] => 17
    [5] => 40
    [6] => 251
    [7] => 251
    [8] => 19
    [9] => 17
    )

    $
    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Hello World

      #3
      Re: add up an integer with the result from substr()

      > $second_int = substr( .... ) + $int;

      $second_int = intval(substr( .... )) + $int;
      should work imho.
      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

      Comment

      • MuffinMan

        #4
        Re: add up an integer with the result from substr()

        | why don't you post what you tried?


        Well, you're right nothing wrong in it but vars were defined outside a
        function which gave a problem... next problem.... next thread..

        Maarten


        Comment

        • Pedro Graca

          #5
          Re: add up an integer with the result from substr()

          MuffinMan wrote:[color=blue]
          > Well, you're right nothing wrong in it but vars were defined outside a
          > function which gave a problem... next problem.... next thread..[/color]

          You didn't make them global now, did you? :-)

          --
          USENET would be a better place if everybody read: : mail address :
          http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
          http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
          http://www.expita.com/nomime.html : to 10K bytes :

          Comment

          • MuffinMan

            #6
            Re: add up an integer with the result from substr()


            | You didn't make them global now, did you? :-)

            I didn't make them global tried to but failed.... Now it's ok.

            Maarten


            Comment

            Working...