Jump to content

Variable variable constant


felixx68

Recommended Posts

I'm don't understand why someone would not know the name of a variable or a constant? Even if so how could they remember the name to set it to a variable variable or a constant define? Can anyone give me an example? What I am referancing to is this statement from

 

http://hudzilla.org/phpwiki/index.php?title=Constants

 

"Finally, constant() is a function that might seem redundant at first, but it makes sense if you give it a chance: it returns the value of a constant. Now, I realise that you can just get the value of a constant by using it directly, e.g. "print MY_CONSTANT;", but how would you accomplish that if you did not know the constant's name? If you were working with a variable, you could use a variable variable, but this is not possible with constants - hence the constant() function."

 

Sry I'm new and that is kinda confusing.

 

Josh ???

Link to comment
Share on other sites

I think what they are implying is that you may, have for instance, 2 constants: foo and bar.

 

Perhaps dependant on user input, or the result of some other variables, you would sometimes want the constant that is foo and another the constant that is bar.

 

I've found variable variables most useful when working with databases - most other times its easier to use arrays.

 

For example, i had made a script which allowed up to 4 people to play a game of blackjack. As you can imagine, the database had fields like : player1, player2, player1_cards, player2_cards, player1_status etc.

 

Either when accessing data from the database, or updating the database, it was much easier to use variable variables:

 

<?php
function get_status($player_no){
$field = 'player.'$player_no.'_status';//for example, if i passed in number 1 for player_no, the variable would contain player1_status
return $$field;// so if i pass player_no as 1, i would, in effect, be echoing the variable $player1_status
}
?>

 

Thats just a completely rough example which was nothing like how it worked. However, i felt given a little bit of context, it might show how you could use them.

 

Link to comment
Share on other sites

To further elaborate, I'll try to make an example....

 

Think of if you had a script or something that used two databases.... something like:

 

<?php
//start config
define('db1_host', 'localhost');
define('db1_user', 'root');
define('db1_pass', 'root');
define('db1_db', 'database');
define('db2_host', 'localhost');
define('db2_user', 'root');
define('db2_pass', 'root');
define('db2_db', 'database2');
//end config
?>

 

And then on some page you have a form or something where you can set the session variable $_SESSION['database'] to 1, for database1, or 2, for database 2.  Then the connection part of the script could do something like this:

 

$prefix = ($_SESSION['database'] == 1) ? 'db1' : 'db2'; //if the variable = 1, prefix is 'db1' else prefix is 'db2'
$host = constant($prefix.'_host'); //i wouldn't ever use constants like this in a switch type situation, but remember this is an example.... I prolly also wouldn't extract each constant to a variable to a constant since that's kind of pointless unless you're gonna get crazy with error checking.
$user = constant($prefix.'_user');
$password = constant($prefix.'_pass');
$database = constant($prefix.'_db');
$link = mysql_connect($host, $user, $password);
if($link) {
if(mysql_select_db($database)) {
echo 'everything went ok.';
}
else {
echo 'Unable to select database!';
}
}
else {
echo 'Could not connect to database!';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.