variables type

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

    variables type

    It's a newbie question...

    Is it possible to type variable in PHP inspite this is a loose type
    language. Or would that be required in some cases?


  • Kimmo Laine

    #2
    Re: variables type

    "Tijs Verwest" <stephane.volle t@bluewin.ch> wrote in message
    news:443c0c23$1 _2@news.bluewin .ch...[color=blue]
    > It's a newbie question...
    >
    > Is it possible to type variable in PHP inspite this is a loose type
    > language.[/color]


    Variables do have some sort of typing but as you know it's very loose. if a
    particular variable happens to be an integer and you assign an array to it,
    it becomes an array. There are ways of detecting the type of a variable and
    there are ways of casting a variable to certain type.

    A little exaple of ways of manipulating the type.
    <?php

    $foo; // Now we have no idea what this is.
    $foo = "bar"; // assigning a string to it makes it a string.
    echo gettype($foo); // this prints the type. string
    $bar = (int)$foo; // now we assign the string into $bar but typecast it to
    int.
    if(is_int($bar) ) echo "$bar is an integer"; // is it int?
    else echo "wtf? $bar is not an int, it's ". get_type($bar);

    ?>

    I recommend you to read this: http://fi2.php.net/manual/en/ref.var.php
    [color=blue]
    > Or would that be required in some cases?[/color]

    It depends on your code. Sometimes it's crucial, sometimes not. If and when
    it is, I use type casts to make sure something is of some type, for example
    when inserting a number intoa database I make sure it's a number to avoid
    errors. A simple type cast takes care of it.

    --
    "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
    spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • Erwin Moller

      #3
      Re: variables type

      Tijs Verwest wrote:
      [color=blue]
      > It's a newbie question...
      >
      > Is it possible to type variable in PHP inspite this is a loose type
      > language. Or would that be required in some cases?[/color]

      Hi,

      PHP has the concept of types, however:

      From manual:
      -----------------
      The type of a variable is usually not set by the programmer; rather, it is
      decided at runtime by PHP depending on the context in which that variable
      is used.
      -----------------

      Read more here:


      Also be sure to follow the link to 'Type Juggling'.

      So, unlike strongtyped languages, in PHP you let the interpretation of the
      type of a variable to the language itself.
      In case of doubt you can ALWAYS cast to the type you want/demand.

      for example:
      $myInt = (int)$unclearVa lue;

      You can also test for types, but that is all in the documentation.

      In my experience, you do need the casting sometimes, but once you get
      familiar with PHP it is easy to spot where.
      If in doubt: just cast.

      Good luck,
      Erwin Moller

      Comment

      • fletch

        #4
        Re: variables type

        I use a notation, whose name I can't remember, to show what type the
        variable should be.

        $iInt
        $aArray
        $bBool
        $mMixed - could be anything
        $sString
        $oObj

        so I can do loops like
        while(list($sFi eldName,$aField Def)=each($aFie lds)){}

        Also, some regulalry used objects are given their own prefix as opposed
        to a general 'o'

        Checking function declarations becomes much easier also.

        Comment

        • Erwin Moller

          #5
          Re: variables type

          fletch wrote:
          [color=blue]
          > I use a notation, whose name I can't remember, to show what type the
          > variable should be.
          >
          > $iInt
          > $aArray
          > $bBool
          > $mMixed - could be anything
          > $sString
          > $oObj
          >
          > so I can do loops like
          > while(list($sFi eldName,$aField Def)=each($aFie lds)){}
          >
          > Also, some regulalry used objects are given their own prefix as opposed
          > to a general 'o'
          >
          > Checking function declarations becomes much easier also.[/color]

          Hi,

          I think that is called Hongarian Notation.
          Not sure either. :-)

          Erwin

          Comment

          • Jerry Stuckle

            #6
            Re: variables type

            Erwin Moller wrote:[color=blue]
            > fletch wrote:
            >
            >[color=green]
            >>I use a notation, whose name I can't remember, to show what type the
            >>variable should be.
            >>
            >>$iInt
            >>$aArray
            >>$bBool
            >>$mMixed - could be anything
            >>$sString
            >>$oObj
            >>
            >>so I can do loops like
            >>while(list($s FieldName,$aFie ldDef)=each($aF ields)){}
            >>
            >>Also, some regulalry used objects are given their own prefix as opposed
            >>to a general 'o'
            >>
            >>Checking function declarations becomes much easier also.[/color]
            >
            >
            > Hi,
            >
            > I think that is called Hongarian Notation.
            > Not sure either. :-)
            >
            > Erwin
            >[/color]

            Close. It's "Hungarian" . :-)


            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Erwin Moller

              #7
              Re: variables type

              Jerry Stuckle wrote:
              [color=blue]
              > Erwin Moller wrote:[color=green]
              >> fletch wrote:
              >>
              >>[color=darkred]
              >>>I use a notation, whose name I can't remember, to show what type the
              >>>variable should be.
              >>>
              >>>$iInt
              >>>$aArray
              >>>$bBool
              >>>$mMixed - could be anything
              >>>$sString
              >>>$oObj
              >>>
              >>>so I can do loops like
              >>>while(list($ sFieldName,$aFi eldDef)=each($a Fields)){}
              >>>
              >>>Also, some regulalry used objects are given their own prefix as opposed
              >>>to a general 'o'
              >>>
              >>>Checking function declarations becomes much easier also.[/color]
              >>
              >>
              >> Hi,
              >>
              >> I think that is called Hongarian Notation.
              >> Not sure either. :-)
              >>
              >> Erwin
              >>[/color]
              >
              > Close. It's "Hungarian" . :-)
              >[/color]

              :-)

              Ok, thx.
              It's not easy being Dutch on the net. ;-)

              Regards,
              Erwin Moller

              Comment

              • Jerry Stuckle

                #8
                Re: variables type

                Erwin Moller wrote:[color=blue]
                > Ok, thx.
                > It's not easy being Dutch on the net. ;-)
                >
                > Regards,
                > Erwin Moller
                >[/color]

                :-)

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...