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?

Vikram Rao
  • 1,068
  • 4
  • 24
  • 62
  • 1
    A function can only have a single return value. The `define_constants()` function returns only `$constant`. – BenM Sep 09 '17 at 18:02
  • And `$constant` will hold the literal strings like `'SITE_ROOT'`, not the actual constants. – arkascha Sep 09 '17 at 18:04
  • @BenM Would you mind giving me the correct code to achieve what I am looking for? – Vikram Rao Sep 09 '17 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 – Prince Adeyemi Sep 09 '17 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. – Ismael Miguel Sep 09 '17 at 18:19
  • FYI you can have an array of constants like this: https://stackoverflow.com/a/12129264/1325575 – The Onin Sep 09 '17 at 21:00

1 Answers1

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!

Prince Adeyemi
  • 724
  • 6
  • 12
  • 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. – Vikram Rao Sep 09 '17 at 20:55
  • @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 – Prince Adeyemi Sep 09 '17 at 21:20