Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Question for() function with define()?

    is there a way to do something like this because this doesn't work?:
    PHP Code:
    <?php
    define
    ("name_1""jilly");
    define("name_2""hilly");
    define("name_3""billy");
    for(
    $i 1;  $i <= 3$i++){
    echo 
    name_.$i;
    }
    ?>
    what is better arrays or defines for this problem?
    and what is safer arrays or defines?
    thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Thats interesting, never tried anything like that before.
    Just use an array. As for security, I suppose you should consider your current scope. If your in a function call or object, the array is not visible outside of it, while the defined constants are accessible globally.
    Hope that helps you!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    you could use an associative array and use

    PHP Code:
    reset($assoc_array);
    while( 
    $row next($assoc_array) ){
     
    // do foo

    AFAIK there is no benefit in using $_constants for security (except the lower change of allocating dangerous data to it)

  • #4
    New Coder
    Join Date
    Sep 2007
    Location
    India
    Posts
    72
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Thumbs up

    You can use eval() function as well...

    PHP Code:
    <?php
    define
    ("name_1""jilly");
    define("name_2""hilly");
    define("name_3""billy");
    for(
    $i 1;  $i <= 3$i++){
    eval(
    "\$name = name_".$i.";");
    echo 
    $name;
    }
    ?>
    cheers
    - Developer from India

    Firefox 2.0

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    there's a constant() function for doing exactly this: http://uk3.php.net/constant

    PHP Code:
    <?php
    define
    ("name_1""jilly"); 
    define("name_2""hilly"); 
    define("name_3""billy");

    for(
    $i 1;  $i <= 3$i++){ 
      echo 
    constant('name_'.$i);
    }
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Thats interesting, never tried anything like that before.
    Just use an array. As for security, I suppose you should consider your current scope. If your in a function call or object, the array is not visible outside of it, while the defined constants are accessible globally.
    Hope that helps you!
    but I'm using a config file for the names.
    is there a way to make it safer using a config file?
    maybe like this:
    config file:
    PHP Code:
    <?php
    if (!defined("Website_In")) {
    die(
    "you are a hacker!!!");
    }
    define("name_1""jilly");  
    define("name_2""hilly");  
    define("name_3""billy"); 
    ?>
    this in the main file:
    PHP Code:
    <?php
    define
    ("Website_In"true);
    for(
    $i 1;  $i <= 3$i++){  
      echo 
    constant('name_'.$i); 

    ?>
    Last edited by shedokan; 12-23-2007 at 05:40 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •