1

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?

6
  • 1
    A function can only have a single return value. The define_constants() function returns only $constant. Commented Sep 9, 2017 at 18:02
  • And $constant will hold the literal strings like 'SITE_ROOT', not the actual constants. Commented Sep 9, 2017 at 18:04
  • @BenM Would you mind giving me the correct code to achieve what I am looking for? Commented Sep 9, 2017 at 18:08
  • You did not define CONSTANT you defined array of strings, and first return was fired return $constant which returns array of strings that was saved in your variable $constant you must use the define to complete the process Commented Sep 9, 2017 at 18:11
  • define only accepts a string with the name of the constant and a scalar value. It doesn't accept arrays. You would have to use an array_map or foreach for that. Commented Sep 9, 2017 at 18:19

1 Answer 1

2

Create Paths Dynamically

With this tiny function, you can create your paths as array of key => value, pass it to the function to create the paths for your application.

  • Create array of paths

    using example in this question -- use right paths

      $path = array(
            'SITE_ROOT'   => $_SERVER['DOCUMENT_ROOT'],
            'CORE_PATH' => '/core',
            'INCLUDE_PATH' => '/inc',
            'LAYOUT_PATH' => '/layout',
            'BLOCK_PATH' => '/blocks',
            'STATIC_PATH' => '/static'
        );
    

    usage create paths using the function:

    createPath( $path );

  • Testing path

    echo CORE_PATH;

  • OUTPUT

    /core/

Create a function to handle paths.

function createPath( $path )
{
    if( empty( $path ) )
    {
        die("Array of path required!");
    }
    foreach( $path as $constant => $path )
    {
        if(!defined( strtoupper($constant) ) )
        {
            // define( strtoupper($constant), $path . '/'); 
            define( strtoupper($constant), realpath( dirname( __FILE__) ) . $path . '/');
        }
    }
}

youpage.php

<?php
/**Create array of paths  array of $constant to $path;
 * i.e $path = array( 'THIS_CONSTANT' => '/this/path', 'WEB_ROOT' => '/path/to/webroot' );
 *  usage:
 *  `createPath( $path );` 
 *  Test: `echo WEB_ROOT;`  OUTPUT: '/path/to/webroot/'
 *
 * - How to Include another scripts:

 * require_once CORE_PATH . 'Config.php';
 * require_once INCLUDE_PATH . 'Database.php';
 * require_once LAYOUT_PATH 'Header.php';
 * require_once LAYOUT_PATH 'Body.php';
 * require_once LAYOUT_PATH 'Footer.php';
*/
 $path = array(
        'SITE_ROOT'   =>   $_SERVER['DOCUMENT_ROOT'],
        'CORE_PATH' => '/core',
        'INCLUDE_PATH' => '/inc',
        'LAYOUT_PATH' => '/layout',
        'BLOCK_PATH' => '/blocks',
        'STATIC_PATH' => '/static'
    );

 //usage:
 createPath( $path );

// Test. You can echo path, include | require e.g:
 echo STATIC_PATH;


function createPath( $path )
{
    if( empty( $path ) )
    {
        die("Array of path required!");
    }
    foreach( $path as $constant => $path )
    {
        if(!defined( strtoupper($constant) ) )
        {
            // define( strtoupper($constant), $path . '/'); 
            define( strtoupper($constant), realpath( dirname( __FILE__) ) . $path . '/');
        }
    }
}

Test a DEMO Version online

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Hi and thanks for trying to help me. I've updated my question ad per your suggestion but it does not work. Can you please update your answer with a working function.
@VikramRao you have to update the constants with your real application path, don't just copy and paste this as I don't know if your application paths used in the above example are your real paths

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.