...

View Full Version : for() function with define()?



shedokan
12-21-2007, 03:37 AM
is there a way to do something like this because this doesn't work?:

<?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.

Fou-Lu
12-21-2007, 05:08 AM
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!

psykx
12-21-2007, 09:37 AM
you could use an associative array and use



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)

nissar
12-21-2007, 09:45 AM
You can use eval() function as well...



<?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

GJay
12-21-2007, 10:43 AM
there's a constant() function for doing exactly this: http://uk3.php.net/constant



<?php
define("name_1", "jilly");
define("name_2", "hilly");
define("name_3", "billy");

for($i = 1; $i <= 3; $i++){
echo constant('name_'.$i);
}

shedokan
12-23-2007, 05:37 PM
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
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
define("Website_In", true);
for($i = 1; $i <= 3; $i++){
echo constant('name_'.$i);
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum