Status
Not open for further replies.

litewarez

Active Member
1,367
2008
1
0
Thought i would share a small tip with you about php that not many people know.

Create a file called config.php

and in that file place the following.

PHP:
return array(
    'db' => array(
        'host' => 'litewarez.com',
        'user' => 'usr'
        //ETC
    ),
);

Then when you need your config just go

PHP:
$config = include 'config.php';
echo $config['db']['host']; // litewarez.com

I bet you never knew that :)
 
9 comments
I used it before. Here's one pratical example from php.net I based something on a while back.

PHP:
<?php
//EXAMPLE  of Multi-Dimentional Array where as an array's keys are an array itself.
//It's so easy to create one like this.

$movie_to_watch = array ('Action'=>
          array('Kanu Reeves' => 'Matrix Reloaded',
                      'Pearce Brosnan' => 'Die Another Day',
                      'Tom Cruz' => 'Mission Impossible',
                      'Jason Statham' => 'Crank',
                      'Danzel Washington' => 'Man on Fire'),
                'Comedy' =>
                array ('Charlie Chaplin' => 'City Lights',
                       'Jim Carrey'    => 'Cable Guy',
                       'Rowan Atkinson' => 'The Ultimate Disaster'));
$type_wanted = 'Action'; //You may switch type from Action to Comedy.
$hero_wanted = 'Pearce Brosnan'; // You may switch hero from Pearce Brosnan to Jim Carrey.

print ("$hero_wanted 's  $type_wanted movie is " . $movie_to_watch[$type_wanted][$hero_wanted].".");
// produces browser output as under:
// Pearce Brosnan 's Action movie is Die Another Day.
?>

As you can see instead of having just the 'db' like Litewarez has you can expand it and have more like 'Action' 'Comedy' and 'Drama' etc. Really useful.
 
How to get PR1?

Well,
My site is1 month oldnow today and its 640K rank on alexa...

Hmm...I would like to know when willit be pr1 :S
 
I used it before. Here's one pratical example from php.net I based something on a while back.

PHP:
<?php
//EXAMPLE  of Multi-Dimentional Array where as an array's keys are an array itself.
//It's so easy to create one like this.

$movie_to_watch = array ('Action'=>
          array('Kanu Reeves' => 'Matrix Reloaded',
                      'Pearce Brosnan' => 'Die Another Day',
                      'Tom Cruz' => 'Mission Impossible',
                      'Jason Statham' => 'Crank',
                      'Danzel Washington' => 'Man on Fire'),
                'Comedy' =>
                array ('Charlie Chaplin' => 'City Lights',
                       'Jim Carrey'    => 'Cable Guy',
                       'Rowan Atkinson' => 'The Ultimate Disaster'));
$type_wanted = 'Action'; //You may switch type from Action to Comedy.
$hero_wanted = 'Pearce Brosnan'; // You may switch hero from Pearce Brosnan to Jim Carrey.

print ("$hero_wanted 's  $type_wanted movie is " . $movie_to_watch[$type_wanted][$hero_wanted].".");
// produces browser output as under:
// Pearce Brosnan 's Action movie is Die Another Day.
?>
As you can see instead of having just the 'db' like Litewarez has you can expand it and have more like 'Action' 'Comedy' and 'Drama' etc. Really useful.

Thanks, but does this example explains multi dimensional array?

I was guessing what litewarez was doing was having these arrays written in a "return" form and could be retrieved with a variable.
 
Yea the tip is creating a php file that returns an array you can then use $variable = include 'some file.php';

And yea this is a multidimensional array lol
 
Returning inclusions can be useful sometimes but in the case of a config file I am unsure.

I'd definately consider it for some kind of array which, for some reason, I want to constantly name differently.
 
Status
Not open for further replies.
Back
Top