Jump to content

[SOLVED] Noobie Variable Defining Question


Zepo.

Recommended Posts

This doesnt work for some reason

 

 

//Directory To You Style

$config[theme] ='default';

 

//Style Initialization

$config        ='./theme/$config[theme]/html.php';

$config[html2] ='./theme/$config[theme]/html2.php';

include("./theme/$config[theme]/styleinfo.php");

Link to comment
https://forums.phpfreaks.com/topic/73224-solved-noobie-variable-defining-question/
Share on other sites

shouldn't make a difference, but put inverted commas round the bracketed arrays:

 

//Directory To You Style

$config['theme']      ='default';

 

//Style Initialization

$config      ='./theme/$config[theme]/html.php';

$config['html2']      ='./theme/$config[theme]/html2.php';

include("./theme/$config[theme]/styleinfo.php");

Try:

 

<?php
$config['theme']='default';
//Style Initialization
$config['html']='./theme/'.$config['theme'].'/html.php';
$config['html2']='./theme/'.$config['theme'].'/html2.php';
include('./theme/'.$config['theme'].'/styleinfo.php');
?>

 

You should quote array keys which are strings - otherwise you will recieve an undefined constant notice if you have error reporting set to include notices.

 

Second, in your previous code you defined $config as an array (implicitly by using the key, 'them'), then changed it to a string, since you gave no key in this line: $config      ='./theme/$config[theme]/html.php';

You then change it back to an array in the following line. The end result: an array with only one element.

 

Lastly, strings inside single quotes are treated as literals. The following code:

<?php
$foo = 'bar';
echo '$foo';
?>

Produces the text: $foo - not the variables contents, bar.

 

When echoing a variable, you either use concatenation(as i did - using the . to 'piece together' the bits of your string), or use double quotes.

 

Your problem is understanding the difference between a string and an array:

 

//Directory To You Style
$config[theme]      ='default';

//Style Initialization
$config           ='./theme/$config[theme]/html.php';
$config[html2]      ='./theme/$config[theme]/html2.php';
include("./theme/$config[theme]/styleinfo.php");

 

First, define $config as an array initially to deal with annoying php.ini files.

Second, $config[theme] needs to have quotes around "theme" or else PHP will interpret that as another variable/undefined constant. So:

 

$config = array();
$config['theme'] = 'default';

 

This part is fine. This means $config is defined as an array and that the index 'theme' is defined as 'default'.

 

Now, look at your second part of the code:

 

//Style Initialization
$config           ='./theme/$config[theme]/html.php';
$config[html2]      ='./theme/$config[theme]/html2.php';
include("./theme/$config[theme]/styleinfo.php");

 

You want to store a string into a variable already defined as an array, this is a 'no-no'. Define another index of $config in order to make it work, IE $config['html']. Also put double quotes around the whole thing so it can be added into the thing correctly.

 

//Style Initialization
$config['html'] = "./theme/".$config['theme']."/html.php";
$config['html2'] = "./theme/".$config['theme']."/html2.php";
include("./theme/".$config['theme']."/styleinfo.php");

 

Finished code:

 

$config = array();
$config['theme'] = 'default';

//Style Initialization
$config['html'] = "./theme/".$config['theme']."/html.php";
$config['html2'] = "./theme/".$config['theme']."/html2.php";
include("./theme/".$config['theme']."/styleinfo.php");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.