{"id":1534,"date":"2021-05-29T16:04:12","date_gmt":"2021-05-29T16:04:12","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=1534"},"modified":"2021-05-31T07:14:11","modified_gmt":"2021-05-31T07:14:11","slug":"parse-ini-file","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/php\/parse-ini-file\/","title":{"rendered":"Parse ini file"},"content":{"rendered":"\n<h3>What is INI file<\/h3>\n\n\n\n<p>An INI file is a configuration file for computer software that consists of a text-based content with a structure and syntax comprising key\u2013value pairs for properties, and sections that organize the properties.<\/p>\n\n\n\n<h3>parse_ini_file function<\/h3>\n\n\n\n<p>Parse ini file is a function to parse any configuration file which has key-value pair<br>This is required when you want to keep all application-level configuration parameters in one place<br>Maintaining configuration level parameter\/variables is easy when you use ini file<br>You need to make changes in one file and it will reflect changes throughout the application<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parse_ini_file(file, process_sections, scanner_mode)<\/code><\/pre>\n\n\n\n<p><em>myapp.ini<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#comment goes here\n\nAPP_NAME\t= UMS\nADMIN_EMAIL\t= admin@umsapp.com\n\n\n#local database credentials\n&#91;localdb]\nDB_HOSTNAME\t= localhost\nDB_USERNAME\t= root\nDB_PASSWORD\t= \"\"\nDB_PORT\t\t= 3306\nDB_NAME\t\t= ecom\n\n\n#production database credentials\n&#91;proddb]\nPRD_DB_HOSTNAME\t= localhost\nPRD_DB_USERNAME\t= root\nPRD_DB_PASSWORD\t= \"\"\nPRD_DB_PORT\t= 3306\nPRD_DB_NAME\t= ecom\n<\/code><\/pre>\n\n\n\n<p><em>global_functions.php<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getConnection() {\n\t\n\t$db = parse_ini_file('config\/myapp.ini', true)&#91;'localdb'];\n\t\n\t$conn = null;\n\t\n\t$servername\t= $db&#91;'DB_HOSTNAME'];\n\t$dbname\t\t= $db&#91;'DB_NAME'];\n\t$username \t= $db&#91;'DB_USERNAME'];\n\t$password\t= $db&#91;'DB_PASSWORD'];\n\t\n\ttry {\n\t  $conn = new PDO(\"mysql:host=$servername;dbname=$dbname\", $username, $password);\n\t  $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\t\n\t} catch(PDOException $e) {\n\t  echo \"Error: \" . $e-&gt;getMessage();\n\t}\n\t\n\treturn $conn;\n}<\/code><\/pre>\n\n\n\n<p>Ref: <a href=\"https:\/\/www.w3schools.com\/php\/func_filesystem_parse_ini_file.asp\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.w3schools.com\/php\/func_filesystem_parse_ini_file.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is INI file An INI file is a configuration file for computer software that consists of a text-based content with a structure and syntax comprising key\u2013value pairs for properties, and sections that organize the properties. parse_ini_file function Parse ini file is a function to parse any configuration file which has key-value pairThis is required [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1534"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=1534"}],"version-history":[{"count":10,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1534\/revisions"}],"predecessor-version":[{"id":1572,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1534\/revisions\/1572"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=1534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=1534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=1534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}