Results 1 to 8 of 8

Thread: $$variable?

  1. #1
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default $$variable?

    Hey I posted this on another forum, but wanted to get a quick response, so here I go, .
    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 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($total2);
    $monthly round($monthly2);
    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:
    PHP Code:
     that would be $<b>$monthly</beach
    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?
    Jeremy | jfein.net

  2. #2
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    Huh? I'm confused, are you saying that \$$var didn't work? Or that you want to know what $$var is?

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    $<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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    It's simple stuff.
    Code:
    $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.
    Last edited by Jas; 02-17-2008 at 08:08 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  5. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Thanks, but I think the other forums are helping me out a little better.
    Jeremy | jfein.net

  6. #6
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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:

    PHP Code:
    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:

    PHP Code:
    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.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Often, sprintf() is useful:
    Code:
    echo sprintf('$%s', $var);
    This is usually a lot easier to read than complex concatenation or interpolation.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    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.
    Code:
    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)

    Edit: What other forum do you belong to?
    Last edited by Jas; 02-17-2008 at 11:03 PM. Reason: typo
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Bookmarks

Posting Permissions

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