SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php exercises help please

    Now starting php exercises from here:

    PHP Beginnings Ex. #5: Variable Content and Destruction | PHP Exercises

    I am not understanding the purpose of this exercise, can anyone tell me in which real application this would be useful please?

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    It's trying to show you... that you can output variable content without ECHO? I guess?
    He's also not destroying the variable - should be using unset() for that. The 'error' reported in the contents is the correct thing - the variable shouldnt exist anymore.

    Not...really the best tutorial.

    anyway. Why would you need this information? Debugging, mainly. var_dump'ing a variable is good for making sure the variable contains what you believe it should contain.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it's not a tutorial, just a bunch of exercises for me to test myself but thanks for the explanation. I don't understand one thing here:

    Code:
    $around = "around";
    echo 'What goes ' . $around .  ' comes ' . $around . '.' ;
    what if you don't want a period at the end of the string like this:

    What goes around comes around

    why does it NOT work if you remove the '.' at the end of the code so it is now?


    Code:
    echo 'What goes ' . $around .  ' comes ' . $around . ;

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Because you are telling PHP to concatenate the var $around to nothing, which is an error, remove the last . and it will work again.

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, how would I add a comma after the word around please so it would show as

    what goes around, comes around

    ??

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Give it your best shot, and post your attempt here.

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have come up with this but it still does not place the comma at the correct spot which is next to the 'd' [last letter in the word 'around'], if you notice the comma is one space away:

    Code:
    $myWord = 'around';
    echo 'What goes ' . $myWord .  ', comes ' . $myWord;

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    "What goes around, comes around"

    Yes it does. Don't worry about white space around the concat dots, all of these will create the same result.
    PHP Code:
    echo 'What goes ' $myWord .  ', comes ' $myWord
    PHP Code:
    echo 'What goes '.$myWord.', comes '.$myWord
    PHP Code:
    echo "What goes $myWord, comes $myWord"
    PHP Code:
    echo 'What goes ' $myWord ,  ', comes ' $myWord
    PHP Code:
    echo 'What goes '                .$myWord.          ', comes '.$myWord

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, it works, thanks for help!

  10. #10
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When using this code:

    Code:
    $date=getdate(date("U"));
    
    if($date[month]=="August")echo "Its August, so its really hot.";
    else echo "Not August, so at least not in the peak of the heat.";
    The output is :

    Notice: Use of undefined constant month - assumed 'month' in C:\xampp\htdocs\index.php on line 9
    Not August, so at least not in the peak of the heat.

    What's wrong here please?

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You need to quote the array key.
    PHP Code:
    <?php
    $date 
    getdate();
    echo 
    $date['month'];
    It's often accepted practice to use date for this, but you may have other reasons why you're using getdate.

    PHP Code:
    <?php
    echo date('F');
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $date['month'] <-this is accessing an array, quote the keys when they are strings - or you get the nasty warning.

    $date[0] <-this is accessing an array, no need to quote integers

  13. #13
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding the exercise:
    Code:
    abc abc abc abc abc abc abc abc abc
    
    xyz xyz xyz xyz xyz xyz xyz xyz xyz
    
    1 2 3 4 5 6 7 8 9
    
        Item A
        Item B
        Item C
        Item D
        Item E
        Item F
    
    Create the 'abc' row with a while loop, the 'xyz' row with a do-while loop, and the last two sections with for loops. Remember to include HTML and source code line breaks in your output. No arrays allowed in this solution.
    I am not understanding a few parts of the code:

    Code:
    echo "<p>\n";
     
    $counter = 1;
    while ($counter < 10){
      echo 'abc ';
      $counter++;
    }
     
    echo "</p>\n";
    echo "<p>\n";


    What's the bolded parts of the code for please?

  14. #14
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    <p> is the HTML code for a paragraph block.
    \n is an escape sequence to put a newline character in the source code, to make it readable.

  15. #15
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, exactly, first time I am seeing the \n, I was wondering how the output was showing on separate lines without the <br/>

  16. #16
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    the -output- is being broken up by the <p>'s.
    the -source code- gets broken up by the \n's.

  17. #17
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, realizing that now, thanks!


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
  •