Log in

View Full Version : $$variable?



Nile
02-17-2008, 06:28 PM
Hey I posted this on another forum, but wanted to get a quick response, so here I go, :p.
I was looking throughout my php book yesterday, and found some little app, this was from my:
PHP For the world wide web, Larry Ullman,
the code is:
PHP Code:

<?php
if(isset($_POST['s'])){
$shipping = $_POST['shipping'];
$pay = $_POST['pay'];
$pr = $_POST['pr'];
$qa = $_POST['qa'];
$ds = $_POST['ds'];
$tx = $_POST['tx'];
$shipping = $_POST['shipping'];
$pay = $_POST['pay'];

$total = $pr * $qa + $shipping - $ds;

$taxrate = $tx/100 + 1;

$total = $taxrate * $total;

$monthly = $total / $pay;

$total = round($total, 2);
$monthly = round($monthly, 2);
print("You have selected to purchase:<br />
<b>$qa</b> widget(s) at <br /> $<b>$pr</b> price with a<br />
$<b>$shipping</b> shipping cost and a<br />
<b>$tx</b> percent tax rate.<br />
After your $<b>$ds</b> discount, the total cost $<b>$total</b>.<br />
Devided over <b>$pay</b> monthly payments, that would be $<b>$monthly</b> each.");
exit;
}
?>
Fill out the forums to calculate the costs: <br />
<form action="#" method="POST">
Price: <input type="text" name="pr" size="5" /><br />
Quantity: <input type="text" name="qa" size="5" /><br />
Discount: <input type="text" name="ds" size="5" /><br />
Tax: <input type="text" name="tx" size="5" /><br />
Shipping Method:
<select name="shipping">
<option value="5.00">Slow & Steady.</option>
<option value="8.00">Put a move on it.</option>
<option value="19.36">I need it yesterday!</option>
</select><br />
Number of payments: <input type="text" name="pay" size="3" /><br />
<input type="submit" name="s" value="Calculate" />
</form>
Now Ill take one line from that code:

that would be $<b>$monthly</b> each.
Now it says in my book:
You can use two methods to print a figure such as $200.00: put a space (or HTML tags, as you do here) between the $ sign and the variable name or escape the first dollar sign(print "\$$var".
You can't use $$variable because the combination of two $ signs creates a var thats to complicated to discuss in this book.

Now I tried the way that was "Two Complex", and it didn't make a diffrence at all, can anyone tell me what it means?

TimFA
02-17-2008, 07:24 PM
Huh? I'm confused, are you saying that \$$var didn't work? Or that you want to know what $$var is?

Twey
02-17-2008, 07:34 PM
$<b> works because < isn't a valid character in identifiers.
You can't use $$variable because the combination of two $ signs creates a var thats to complicated to discuss in this book.It's not really complicated. I suggest you get a new book -- this one doesn't seem to be a shining example of the genre.

Jas
02-17-2008, 07:57 PM
It's simple stuff.


$var = 'hello';
$$var = 'hi'; //create a var, the name of which is the value of $var
print $var; //outputs hello
print $hello; //outputs hi

See here (http://us2.php.net/manual/en/language.variables.variable.php).

Nile
02-17-2008, 08:13 PM
Thanks, but I think the other forums are helping me out a little better.
:p

alexjewell
02-17-2008, 08:19 PM
Very simply put: $$ in front of a variable name makes that variable's name whatever the original variable equals. If $color = 'red'; then $$color is really the variable $red. The book you're reading is, in fact, one I own...however, it is not a good resource by itself for learning PHP; it is only useful in conjunction with other PHP resources.

Now, it is right about escaping the dollar sign. Another way to do this is to use single quotes:



echo '$'.$var;


The backslash method, the one you touched on, also works but can get ugly depending on how many symbols you have to backslash:



echo "\$$var";


Anyway, I know Twey and Jas already answered, but I wanted to go a little bit more in-depth to make sure you understood entirely what was going on and why. If you have any other questions, feel free to let me know. And, by the way, you were smart in consulting several places for answers...you learn the most from communication with more people.

Twey
02-17-2008, 09:42 PM
Often, sprintf() is useful:
echo sprintf('$%s', $var);This is usually a lot easier to read than complex concatenation or interpolation.

Jas
02-17-2008, 10:41 PM
Not to repeat what everyone else already said, but the bottom line is that you don't want the PHP parser to think that it is a "variable variable," which is a type of variable that I showed in my last post. In other words, you have to eleminate one $ by any normal means. Single qoutes ( ' ), back slashes (\), concating, ect.

print "\$$var"; //prints $ + the value of $var, not $$var
print '$$var'; //prints $$var
print '$'.$var; //prints $+ the value of $var
print "$<b>$var</b>"; //prints $<b>+value of $var+</b>
//This is because a var can't start with <, so $< can't be a var
And so on. Two of these $ equals a variable variable. So $$var != $var, inside or outside of ""

(sorry if this seems like an echo)

What other forum do you belong to?