variables and values

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

    variables and values

    hi all, i'm very new to php and i'm a little confused about something.
    this will be dead simple to the rest of you but it doesn't work for
    me.

    i have a variable called "$a" which has a value of 10.

    i want to pass this value to a new variable and increase it by 10, is
    this the right way?

    $a=$b;
    $b + 10;

    thanks for not laughing :o)
  • Ian.H

    #2
    Re: variables and values

    On Fri, 02 Apr 2004 10:31:47 -0800, none wrote:
    [color=blue]
    > hi all, i'm very new to php and i'm a little confused about something.
    > this will be dead simple to the rest of you but it doesn't work for
    > me.
    >
    > i have a variable called "$a" which has a value of 10.
    >
    > i want to pass this value to a new variable and increase it by 10, is
    > this the right way?
    >
    > $a=$b;
    > $b + 10;
    >
    > thanks for not laughing :o)[/color]


    What you have is perfectly "legal" and will work.. however, if you want to
    tidy it up:


    $b = $a + 10;


    If you don't _really_ need to pass it to another value and just +10 to it,
    you can use:


    $a +=10;


    This is the same as:


    $a = $a + 10;


    I realise this might not be what you're after (second example) but may be
    of use for the future if you were unaware =)


    HTH.



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK


    Comment

    • Pedro Graca

      #3
      Re: variables and values


      Hi none! Welcome to PHP.

      none wrote:[color=blue]
      > i have a variable called "$a" which has a value of 10.
      >
      > i want to pass this value to a new variable and increase it by 10, is
      > this the right way?
      >
      > $a=$b;[/color]

      This puts into $a the contents of $b;

      How did $a get to have the value of 10?
      You didn't do "10=$a;" (parse error) -- this doesn't work because you
      can't change the value 10. The 'thing' being changed _always_ (*) goes
      on the left side of the equals sign.

      To make $b have the same thing $a has do

      $b = $a;

      [color=blue]
      > $b + 10;[/color]

      This one is not changing any variable -- it just calculates the value
      and then ignores it because there is no instruction to do something with
      that value; you want to put that value into a variable, so you have to
      say that to PHP:

      $b = $b + 10;

      Which means "add the contents of $b to 10 and save the resulting value
      in $b".


      (*) but some things can be changed on the right side too, or even
      without an equals sign :)

      --
      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

      • Ian.H

        #4
        Re: variables and values

        On Fri, 02 Apr 2004 18:42:55 +0000, Ian.H wrote:
        [color=blue]
        > On Fri, 02 Apr 2004 10:31:47 -0800, none wrote:
        >[color=green]
        >> hi all, i'm very new to php and i'm a little confused about something.
        >> this will be dead simple to the rest of you but it doesn't work for
        >> me.
        >>
        >> i have a variable called "$a" which has a value of 10.
        >>
        >> i want to pass this value to a new variable and increase it by 10, is
        >> this the right way?
        >>
        >> $a=$b;
        >> $b + 10;
        >>
        >> thanks for not laughing :o)[/color]
        >
        >
        > What you have is perfectly "legal" and will work.. however, if you want to
        > tidy it up:
        >
        >
        > $b = $a + 10;[/color]


        Oops!

        After reading Pedro's post below.. I realised I made a massive error here =[

        Pedro is correct.. please excuse and ignore my reply about the above being
        correct.

        The following _is_ correct (just for my own FWIW =) ):


        $b = $a;
        $b =+ 10;

        $b = $a + 10;

        $b = $a;
        $b = $b + 10;


        All of these will work.. but your choice which is preferable (#2) =)



        Regards,

        Ian

        --
        Ian.H
        digiServ Network
        London, UK


        Comment

        • Andy Hassall

          #5
          Re: variables and values

          On Fri, 02 Apr 2004 19:30:39 GMT, "Ian.H" <ian@WINDOZEdig iserv.net> wrote:
          [color=blue]
          >The following _is_ correct (just for my own FWIW =) ):
          >
          > $b =+ 10;[/color]

          *polite cough* ;-) Isn't that:

          '$b' 'assignment' 'unary positive' 'constant integer 10' ?

          You mean $b += 10...

          --
          Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
          http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

          Comment

          • Chung Leong

            #6
            }Re: variables and values


            "none" <kinskai@aol.co m> wrote in message
            news:863d38a1.0 404021031.59d29 d33@posting.goo gle.com...[color=blue]
            > hi all, i'm very new to php and i'm a little confused about something.
            > this will be dead simple to the rest of you but it doesn't work for
            > me.
            >
            > i have a variable called "$a" which has a value of 10.
            >
            > i want to pass this value to a new variable and increase it by 10, is
            > this the right way?
            >
            > $a=$b;
            > $b + 10;
            >
            > thanks for not laughing :o)[/color]

            This is the right way to do it, I think:

            class Number {
            $value;

            function Number($value) {
            $this->value = $value;
            }

            function ToString() {
            return $this->value;
            }

            function GetValue() {
            return $this->value;
            }

            function Clone() {
            return $this;
            }
            }

            class RealInteger extends Number {
            function RealInteger($va lue) {
            $this->Number($value) ;
            }

            function IsPositive() {
            return ($this->value >= 0);
            }

            function Add($increment) {
            $this->value += $increment;
            return $this->value;
            }

            function Minus($incremen t) {
            $this->value -= $increment;
            return $this->value;
            }
            }

            class CustomRealInteg er extends RealInteger {
            function CustomRealInteg er($value = 0) {
            $this->RealInteger($v alue);
            }

            function IncreaseByTen() {
            return $this->Add(10);
            }
            }

            $a = new CustomRealInteg er(5);
            $b = $a->Clone();
            $b->IncreaseByTen( );


            Comment

            • none

              #7
              Re: variables and values

              Pedro Graca <hexkid@hotpop. com> wrote in message news:<c4kcee$2j 162r$1@ID-203069.news.uni-berlin.de>...[color=blue]
              > Hi none! Welcome to PHP.
              >
              > none wrote:[color=green]
              > > i have a variable called "$a" which has a value of 10.
              > >
              > > i want to pass this value to a new variable and increase it by 10, is
              > > this the right way?
              > >
              > > $a=$b;[/color]
              >
              > This puts into $a the contents of $b;
              >
              > How did $a get to have the value of 10?
              > You didn't do "10=$a;" (parse error) -- this doesn't work because you
              > can't change the value 10. The 'thing' being changed _always_ (*) goes
              > on the left side of the equals sign.
              >
              > To make $b have the same thing $a has do
              >
              > $b = $a;
              >
              >[color=green]
              > > $b + 10;[/color]
              >
              > This one is not changing any variable -- it just calculates the value
              > and then ignores it because there is no instruction to do something with
              > that value; you want to put that value into a variable, so you have to
              > say that to PHP:
              >
              > $b = $b + 10;
              >
              > Which means "add the contents of $b to 10 and save the resulting value
              > in $b".
              >
              >
              > (*) but some things can be changed on the right side too, or even
              > without an equals sign :)[/color]


              Beuty, all working!!!

              Comment

              • Filth

                #8
                Re: variables and values

                [color=blue]
                > This is the right way to do it, I think:
                >
                > class Number {
                > $value;
                >
                > function Number($value) {
                > $this->value = $value;
                > }
                >
                > function ToString() {
                > return $this->value;
                > }
                >
                > function GetValue() {
                > return $this->value;
                > }
                >
                > function Clone() {
                > return $this;
                > }
                > }
                >
                > class RealInteger extends Number {
                > function RealInteger($va lue) {
                > $this->Number($value) ;
                > }
                >
                > function IsPositive() {
                > return ($this->value >= 0);
                > }
                >
                > function Add($increment) {
                > $this->value += $increment;
                > return $this->value;
                > }
                >
                > function Minus($incremen t) {
                > $this->value -= $increment;
                > return $this->value;
                > }
                > }
                >
                > class CustomRealInteg er extends RealInteger {
                > function CustomRealInteg er($value = 0) {
                > $this->RealInteger($v alue);
                > }
                >
                > function IncreaseByTen() {
                > return $this->Add(10);
                > }
                > }
                >
                > $a = new CustomRealInteg er(5);
                > $b = $a->Clone();
                > $b->IncreaseByTen( );[/color]

                dont you think that is a bit overkill for someone who just wants to know how
                to add 10 to a variable


                Comment

                • Agelmar

                  #9
                  Re: variables and values

                  Chung Leong wrote:[color=blue]
                  > "none" <kinskai@aol.co m> wrote in message
                  > news:863d38a1.0 404021031.59d29 d33@posting.goo gle.com...[color=green]
                  >> hi all, i'm very new to php and i'm a little confused about
                  >> something. this will be dead simple to the rest of you but it
                  >> doesn't work for me.
                  >>
                  >> i have a variable called "$a" which has a value of 10.
                  >>
                  >> i want to pass this value to a new variable and increase it by 10, is
                  >> this the right way?
                  >>
                  >> $a=$b;
                  >> $b + 10;
                  >>
                  >> thanks for not laughing :o)[/color]
                  >
                  > This is the right way to do it, I think:
                  >
                  > class Number {[/color]
                  <snip>

                  LMFAO, I can't stop laughing...


                  Comment

                  • Ian.H

                    #10
                    Re: variables and values

                    On Fri, 02 Apr 2004 21:08:24 +0100, Andy Hassall wrote:
                    [color=blue]
                    > On Fri, 02 Apr 2004 19:30:39 GMT, "Ian.H" <ian@WINDOZEdig iserv.net> wrote:
                    >[color=green]
                    >>The following _is_ correct (just for my own FWIW =) ):
                    >>
                    >> $b =+ 10;[/color]
                    >
                    > *polite cough* ;-) Isn't that:
                    >
                    > '$b' 'assignment' 'unary positive' 'constant integer 10' ?
                    >
                    > You mean $b += 10...[/color]


                    Wow! today _really_ wasn't my day lol.

                    Thanks for pointing out this (additional) error. I'm just glad I post here
                    fairly frequently with some working answers.. I'd look a right prick
                    otherwise.. or a m$ employee (don't they fix bugs with more bugs? =) ).

                    Thanks Andy for pointing this out.. and you're correct, I did mean '+='
                    rather than '=+'.

                    Can I just pretent that $a was defined as 0? =D



                    Regards,

                    Ian

                    --
                    Ian.H
                    digiServ Network
                    London, UK


                    Comment

                    • Jeffrey Silverman

                      #11
                      Re: variables and values

                      On Sat, 03 Apr 2004 12:49:23 +0100, Filth wrote:
                      [color=blue]
                      > dont you think that is a bit overkill for someone who just wants to know how
                      > to add 10 to a variable[/color]

                      For the humor impaired, Chung's post was pretty likely a joke.

                      --
                      Jeffrey D. Silverman | jeffrey AT jhu DOT edu
                      Website | http://www.wse.jhu.edu/newtnotes/

                      Comment

                      Working...