·  PHP – Terms – Files – Properties – Read

 

- Info:

  - This tutorial shows how to use PHP to read properties file.

  - This tutorial is based on http://www.php.net/parse_ini_file.

 

- Procedure:

  - Create PHP Console Application Test.php.

 

    properties.txt

    ;Sample configuration file.

   

    [first_section]

    one    = 1

    five   = 5        ;Some comment.

    animal = BIRD

   

    [second_section]

    path   = "/usr/local/bin"

    URL    = "http://www.examp

 

    Test.php

    <?php

   

      //Load properties values withouth using section.

      $parameters = parse_ini_file("properties.txt");

      if($parameters==false) {

        print("Unable to open properties file.");

        exit(1);

      }

      

      //Display properties values.

      echo($parameters["animal"]."</br>");    //BIRD

      echo($parameters["URL"]);               //http://www.example.com/~username

      

      //Display complete structure of loaded values.

      print_r($parameters);

   

    ?>

 

    Test.php

    <?php

      

      //Load properties values using sections.

      $parameters = parse_ini_file("properties.txt",true);

      if($parameters==false) {

        print("Unable to open properties file.");

        exit(1);

      }

      

      //Display properties values.

      echo($parameters["first_section" ]["animal"]."</br>");  //BIRD

      echo($parameters["second_section"]["URL"   ]."</br>");  //http://www.ivoronline.com/~username

      

      //Display complete structure of loaded values.   

      print_r($parameters);

      

    ?>