Hi I am really bad and a total newbie to PHP. Need some help.
I am trying to define a few constants in my site:
Code 1
define('SITE_ROOT',$_SERVER['DOCUMENT_ROOT'] . '/');
// Check if CORE_PATH is already defined else define here
defined('CORE_PATH') or define('CORE_ROOT', SITE_ROOT . '/CORE');
define('INC_PATH', IAO_ROOT . '/inc/');
define('LAYOUTS_PATH', IAO_ROOT . 'layouts/');
define('BLOCKS_PATH', SECTIONS_PATH . 'blocks/');
define('STATIC_PATH', BLOCKS_PATH . 'static/');
Apart from the above example I have another 10-15 more constants to define. I want to know is it correct to define each constant in one line each or can I do something like below:
Code 2
define (
$constant = array (
'SITE_ROOT',
'CORE_PATH',
'INC_PATH' ,
'LAYOUTS_PATH',
'BLOCKS_PATH',
'STATIC_PATH'
),
$path = array(
$_SERVER['DOCUMENT_ROOT'] . '/',
SITE_ROOT . '/CORE',
CORE_PATH . '/inc',
CORE_PATH . '/layout',
CORE_PATH . '/blocks',
CORE_PATH . '/static'
)
);
define ( $constant, $path);
While Code 1 is working fine on my site, Code 2 is not working for me. Kindly advise me what is the correct way.
UPDATE: Updated this question as per @LasVegasCoder. does not work.
<?php
//Create array of paths --example from your path ***use right paths***;
$path = array(
'SITE_ROOT . ' => $_SERVER['DOCUMENT_ROOT'],
'CORE_PATH' => SITE_ROOT . '/core',
'INCLUDE_PATH' => SITE_ROOT . '/inc',
'LAYOUT_PATH' => SITE_ROOT . '/layout',
'BLOCK_PATH' => SITE_ROOT . '/blocks',
'STATIC_PATH' => SITE_ROOT . '/static'
);
//usage:
createPath( $path );
//Testiing
echo SITE_ROOT; ?></br>
<?php echo CORE_PATH; ?></br>
<?php echo INCLUDE_PATH; ?></br>
<?php echo LAYOUT_PATH; ?></br>
<?php echo BLOCK_PATH; ?></br>
<?php echo STATIC_PATH; ?></br>
<?php
function createPath( $path )
{
if( empty( $path ) )
{
die("Array of path required!");
}
foreach( $path as $constant => $path )
{
if(!defined( strtoupper($constant) ) )
{
define( strtoupper($constant), $path . '/');
}
}
}
Well still it does not work. Any idea and solutions?