Checking PHP version for compatibility?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rustysmith@bellsouth.net

    Checking PHP version for compatibility?

    Anyone know how to check the PHP version that a current file is
    running on? This would be helpful in the instance where you are trying
    to make a package that can be distributed publicly for use in a variety
    of configurations. This code should(?) work:

    $ver=PHP_VERSIO N;
    if(ereg('^[1-4]',$ver))
    $post_ids=array _flip(&$post_id s); // Old syntax
    else
    $post_ids=array _flip($post_ids ); // PHP 5+ syntax

    but it still throws the error:

    Warning: Call-time pass-by-reference has been deprecated - argument
    passed by value; If you would like to pass it by reference, modify the
    declaration of array_flip(). If you would like to enable call-time
    pass-by-reference, you can set allow_call_time _pass_reference to true
    in your INI file. However, future versions may not support this any
    longer.

    When executed in PHP 5.+

    Thanks in advance for any ideas how to make this work?

  • Chris Hope

    #2
    Re: Checking PHP version for compatibility?

    rustysmith@bell south.net wrote:
    Anyone know how to check the PHP version that a current file is
    running on? This would be helpful in the instance where you are trying
    to make a package that can be distributed publicly for use in a
    variety
    of configurations. This code should(?) work:
    >
    $ver=PHP_VERSIO N;
    if(ereg('^[1-4]',$ver))
    $post_ids=array _flip(&$post_id s); // Old syntax
    else
    $post_ids=array _flip($post_ids ); // PHP 5+ syntax
    >
    but it still throws the error:
    >
    Warning: Call-time pass-by-reference has been deprecated - argument
    passed by value; If you would like to pass it by reference, modify the
    declaration of array_flip(). If you would like to enable call-time
    pass-by-reference, you can set allow_call_time _pass_reference to true
    in your INI file. However, future versions may not support this any
    longer.
    >
    When executed in PHP 5.+
    >
    Thanks in advance for any ideas how to make this work?
    I don't get the error when running either of your calls using PHP 5.1.2

    As far as testing for the version, why not just do it like this instead
    of using a regular expression:

    if(PHP_VERSION >= 5) {
    ... php 5 code here ...
    }
    else {
    ... earlier versions code here ...
    }

    --
    Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

    Comment

    • Rusty

      #3
      Re: Checking PHP version for compatibility?

      Thanks for the quick reply Chris - I tried it as you wrote and still
      get the same warning. To get rid of the warning I get rid of the "&",
      but then I do not think it will work with PHP 4.0. There must be
      something up with our PHP setup (windows IIS) - I will check with our
      server folks in the AM - but again a serious thanks again for the help
      - this wil have me a little further down the raod in the morning!

      Rusty

      Chris Hope wrote:
      rustysmith@bell south.net wrote:
      >
      Anyone know how to check the PHP version that a current file is
      running on? This would be helpful in the instance where you are trying
      to make a package that can be distributed publicly for use in a
      variety
      of configurations. This code should(?) work:

      $ver=PHP_VERSIO N;
      if(ereg('^[1-4]',$ver))
      $post_ids=array _flip(&$post_id s); // Old syntax
      else
      $post_ids=array _flip($post_ids ); // PHP 5+ syntax

      but it still throws the error:

      Warning: Call-time pass-by-reference has been deprecated - argument
      passed by value; If you would like to pass it by reference, modify the
      declaration of array_flip(). If you would like to enable call-time
      pass-by-reference, you can set allow_call_time _pass_reference to true
      in your INI file. However, future versions may not support this any
      longer.

      When executed in PHP 5.+

      Thanks in advance for any ideas how to make this work?
      >
      I don't get the error when running either of your calls using PHP 5.1.2
      >
      As far as testing for the version, why not just do it like this instead
      of using a regular expression:
      >
      if(PHP_VERSION >= 5) {
      ... php 5 code here ...
      }
      else {
      ... earlier versions code here ...
      }
      >
      --
      Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

      Comment

      • Jerry Stuckle

        #4
        Re: Checking PHP version for compatibility?

        rustysmith@bell south.net wrote:
        Anyone know how to check the PHP version that a current file is
        running on? This would be helpful in the instance where you are trying
        to make a package that can be distributed publicly for use in a variety
        of configurations. This code should(?) work:
        >
        $ver=PHP_VERSIO N;
        if(ereg('^[1-4]',$ver))
        $post_ids=array _flip(&$post_id s); // Old syntax
        else
        $post_ids=array _flip($post_ids ); // PHP 5+ syntax
        >
        but it still throws the error:
        >
        Warning: Call-time pass-by-reference has been deprecated - argument
        passed by value; If you would like to pass it by reference, modify the
        declaration of array_flip(). If you would like to enable call-time
        pass-by-reference, you can set allow_call_time _pass_reference to true
        in your INI file. However, future versions may not support this any
        longer.
        >
        When executed in PHP 5.+
        >
        Thanks in advance for any ideas how to make this work?
        >
        That's because this is a compile-time notice, not a run-time one.

        array_flip() does not take a reference to the array, it is expecting a
        copy. So you should just use

        $post_ids=array _flip($post_ids );

        in either version.

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

        Comment

        • Mateusz Markowski

          #5
          Re: Checking PHP version for compatibility?


          rustysmith@bell south.net napisal(a):
          Anyone know how to check the PHP version that a current file is
          running on? This would be helpful in the instance where you are trying
          to make a package that can be distributed publicly for use in a variety
          of configurations. This code should(?) work:
          >
          $ver=PHP_VERSIO N;
          if(ereg('^[1-4]',$ver))
          $post_ids=array _flip(&$post_id s); // Old syntax
          else
          $post_ids=array _flip($post_ids ); // PHP 5+ syntax
          >
          For version comapring you should write:

          if (version_compar e(phpversion(), '4.0.0', '>='))
          //version above 4.0.0
          else
          //version below 4.0.0

          But it won't work in a way you want. For example:
          <?php

          if (version_compar e(phpversion(), '5.0.0', '>=')){
          //PHP5
          class Foo{
          private $foobar;
          public function __construct(){
          echo 'Foo';
          }
          }
          }else{
          //PHP5
          class Foo{
          var $foobar;
          function Foo(){
          echo 'Foo';
          }
          }
          }
          ?>
          The PHP4's parser will give you a parse error.
          If you want to be compatibile with both versions, just write in PHP4.

          Comment

          • Colin Fine

            #6
            Re: Checking PHP version for compatibility?

            Mateusz Markowski wrote:
            rustysmith@bell south.net napisal(a):
            >Anyone know how to check the PHP version that a current file is
            >running on? This would be helpful in the instance where you are trying
            >to make a package that can be distributed publicly for use in a variety
            >of configurations. This code should(?) work:
            >>
            >$ver=PHP_VERSI ON;
            > if(ereg('^[1-4]',$ver))
            > $post_ids=array _flip(&$post_id s); // Old syntax
            > else
            > $post_ids=array _flip($post_ids ); // PHP 5+ syntax
            >>
            >
            For version comapring you should write:
            >
            if (version_compar e(phpversion(), '4.0.0', '>='))
            //version above 4.0.0
            else
            //version below 4.0.0
            >
            But it won't work in a way you want. For example:
            <?php
            >
            if (version_compar e(phpversion(), '5.0.0', '>=')){
            //PHP5
            class Foo{
            private $foobar;
            public function __construct(){
            echo 'Foo';
            }
            }
            }else{
            //PHP5
            class Foo{
            var $foobar;
            function Foo(){
            echo 'Foo';
            }
            }
            }
            ?>
            The PHP4's parser will give you a parse error.
            If you want to be compatibile with both versions, just write in PHP4.
            >
            But then you have to turn NOTICES off (not necessarily in this case, but
            in some incompatibiliti es).
            I've ended up taking my own copy of PEAR::Date and editing it to declare
            all the static functions as static in order to use it in PHP5 without
            notices.
            I can't believe this is the right way to deal with the problem, but I
            don't know what else to do.

            Colin

            Comment

            • Jerry Stuckle

              #7
              Re: Checking PHP version for compatibility?

              Colin Fine wrote:
              Mateusz Markowski wrote:
              >
              >rustysmith@bell south.net napisal(a):
              >>
              >>Anyone know how to check the PHP version that a current file is
              >>running on? This would be helpful in the instance where you are trying
              >>to make a package that can be distributed publicly for use in a variety
              >>of configurations. This code should(?) work:
              >>>
              >>$ver=PHP_VERS ION;
              >> if(ereg('^[1-4]',$ver))
              >> $post_ids=array _flip(&$post_id s); // Old syntax
              >> else
              >> $post_ids=array _flip($post_ids ); // PHP 5+ syntax
              >>>
              >>
              >For version comapring you should write:
              >>
              > if (version_compar e(phpversion(), '4.0.0', '>='))
              > //version above 4.0.0
              > else
              > //version below 4.0.0
              >>
              >But it won't work in a way you want. For example:
              ><?php
              >>
              > if (version_compar e(phpversion(), '5.0.0', '>=')){
              > //PHP5
              > class Foo{
              > private $foobar;
              > public function __construct(){
              > echo 'Foo';
              > }
              > }
              > }else{
              > //PHP5
              > class Foo{
              > var $foobar;
              > function Foo(){
              > echo 'Foo';
              > }
              > }
              > }
              >?>
              >The PHP4's parser will give you a parse error.
              >If you want to be compatibile with both versions, just write in PHP4.
              >>
              But then you have to turn NOTICES off (not necessarily in this case, but
              in some incompatibiliti es).
              I've ended up taking my own copy of PEAR::Date and editing it to declare
              all the static functions as static in order to use it in PHP5 without
              notices.
              I can't believe this is the right way to deal with the problem, but I
              don't know what else to do.
              >
              Colin
              Maybe put the level-specific code in two different files and include the
              appropriate file based on the PHP version?

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

              Comment

              • Colin Fine

                #8
                Re: Checking PHP version for compatibility?

                Jerry Stuckle wrote:
                Colin Fine wrote:
                >Mateusz Markowski wrote:
                >>
                >>rustysmith@bell south.net napisal(a):
                >>>
                >>>Anyone know how to check the PHP version that a current file is
                >>>running on? This would be helpful in the instance where you are trying
                >>>to make a package that can be distributed publicly for use in a variety
                >>>of configurations. This code should(?) work:
                >>>>
                >>>$ver=PHP_VER SION;
                >>> if(ereg('^[1-4]',$ver))
                >>> $post_ids=array _flip(&$post_id s); // Old syntax
                >>> else
                >>> $post_ids=array _flip($post_ids ); // PHP 5+ syntax
                >>>>
                >>>
                >>For version comapring you should write:
                >>>
                >> if (version_compar e(phpversion(), '4.0.0', '>='))
                >> //version above 4.0.0
                >> else
                >> //version below 4.0.0
                >>>
                >>But it won't work in a way you want. For example:
                >><?php
                >>>
                >> if (version_compar e(phpversion(), '5.0.0', '>=')){
                >> //PHP5
                >> class Foo{
                >> private $foobar;
                >> public function __construct(){
                >> echo 'Foo';
                >> }
                >> }
                >> }else{
                >> //PHP5
                >> class Foo{
                >> var $foobar;
                >> function Foo(){
                >> echo 'Foo';
                >> }
                >> }
                >> }
                >>?>
                >>The PHP4's parser will give you a parse error.
                >>If you want to be compatibile with both versions, just write in PHP4.
                >>>
                >But then you have to turn NOTICES off (not necessarily in this case,
                >but in some incompatibiliti es).
                >I've ended up taking my own copy of PEAR::Date and editing it to
                >declare all the static functions as static in order to use it in PHP5
                >without notices.
                >I can't believe this is the right way to deal with the problem, but I
                >don't know what else to do.
                >>
                >Colin
                >
                Maybe put the level-specific code in two different files and include the
                appropriate file based on the PHP version?
                >
                Well I guess that might do for the original poster, who wants to provide
                a package that will work with either version; though it seems a horribly
                messy way of handling an obvious problem.

                For my purposes, all I want to do is use an existing PEAR class in PHP5.
                AFAIK this class is not available in a PHP5 version, so the best I have
                thought of doing, as I say, is taking my own copy and editing it.

                Now, there may be some PEAR packages which are so dependent on the way
                PHP4 works that they will require a rewrite for PHP5: but the package I
                am using (Date) seems to require only that a number of functions be
                declared 'static'.
                Given this, is seems a pity - and surprising - that PHP apparently
                offers no way that the writer could make the package acceptable to both
                PHP4 and 5. Or is there such a way, that I'm missing?

                Colin

                Comment

                Working...