SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy please clarify this for me

    Hi,
    I feel stupid having to ask this - but if I don't have it explained I will never learn properly ..so .. Could someone please clarify a few things for me in very (very very) plain english. I know where to put them in the code but don't really understand specifically what they do.


    PHP Code:
    ($row mysql_fetch_array($result) ) 
    PHP Code:
    mysql_num_rows($query

    also this which I don't really understand at all:
    PHP Code:
    $_GET 
    thanks.
    coiL
    "cradled in the learning curve"

  2. #2
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The first example you gave, basically takes the result of your database query, and places them into the array called $row from your example. You would use a while loop to cycle through them.

    The second example counts the number of rows that your query returned. So if you did "SELECT * FROM table" where your table have 1001 record, then if you did

    PHP Code:
    //Sometime the $query part is left out
    echo mysql_num_rows($query); 
    It would print 1001.

    In your last example, $_GET is a super global variable. It contains any information that a URL or form sent to the script by the GET method. You can also send by the POST method.

    PHP Code:
    <?php
    if(empty($_POST['fname'])) {
    ?>
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="fname">
    </form>
    <?php
    } else {
    echo 
    $_POST['fname'];
    }
    Say you had the form above and submitted the form. The value you put in the text box ('fname') will be sent to the script. That value will be stored in $_POST['fname'], where the text between the brackets is the name of the variable that was sent. If you sent via the GET method, it would be $_GET['fname'].

    Just FYI, $_SERVER is another one of these special varialbes, and the value of $_SERVER['PHP_SELF'] is the name of the script that is currently running.

    Hope this helped a little bit,
    -Peter

  3. #3
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks that helped me a bit

    while I'm here , I was wondering what is the correct way to write an echo statement? I've seen it written in a few ways and was wondering which was correct?

    PHP Code:
    echo("Hello world!");
    echo 
    "Hello World!"
    coiL
    "cradled in the learning curve"

  4. #4
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can do it any way really, though this is recommended:

    PHP Code:
    echo 'My name is Peter'
    Notice the single quotes instead of double. To print a variable you would do the following:

    PHP Code:
    $var 'Hello!';
    echo 
    $var
    And to print both a string and a variable at the same time you would do this:

    PHP Code:
    $var 'Hello!';
    echo 
    $var ' My Name is Peter!'

  5. #5
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok Thanks
    I'll have to get used to doing it like that

    is this right?
    PHP Code:
    $username $row["username"]; 
    or should it be:
    PHP Code:
    $username $row['username']; 
    Last edited by coiL; Dec 31, 2002 at 00:41.
    coiL
    "cradled in the learning curve"

  6. #6
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Use the second one.

    -Peter

  7. #7
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks again
    coiL
    "cradled in the learning curve"

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by petesmc
    And to print both a string and a variable at the same time you would do this:

    PHP Code:
    $var 'Hello!';
    echo 
    $var ' My Name is Peter!'
    But... that's what double quotes are for.
    PHP Code:
    $var 'Hello!';
    echo 
    "$var My Name is Peter!"

  9. #9
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should remain consistent with how you print them out, if you are constantly switching between double and single it gets confusing. Also, then when you have to print a record of an array you end up doing:

    PHP Code:
    echo "HI there {$name[0]}"
    Instead of sticking to the same method which would be:

    PHP Code:
    echo 'Hi there ' $name[0]; 
    Not to mention, single quotes are faster...

    -Peter

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by petesmc
    Not to mention, single quotes are faster...
    In this case the difference would be negligible, but I do agree with you that the general rule is to use the least powerful quoting technique necessary.

    However, you can't always use single-quoted strings. For example, the newline character can only be represented in a double-quoted string. And what if you wanted a multiline string? Then you'd need
    PHP Code:
    $foo = <<< foo_bar
    some pointless text
    and another line
    ooh, the third line
    foo_bar; 

  11. #11
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the problem with the single quote method. But to get around that i just do this:
    PHP Code:
    <?php
    define
    ("CR","\n");
    echo 
    'Hi there!' CR 'My name is Peter!';
    ?>
    Also, in function parameters, I've personally found double quotes to be faster for some reason. Of course I tested this by funny a function several time, but it took 10 million calls to a function to notice any difference in speed. Personally, i use double quotes for function parameters, and single for variables and echo.

    -Peter

  12. #12
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From close to the end of http://www.sitepointforums.com/showt...threadid=54074
    Originally posted by MattR
    I think the official word from Zend (et al) is that you should abandon the use of double-quotes since the process to find variables in the quotes is quite costly. Not to mention it craps out quite soon (you can run benchmarks and it will eventually kill PHP).
    Can't say I have personally run tests to try that out but it is generally in sync with what I have read in the past.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  13. #13
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Coil:
    Although you seem to be getting this stuff pretty quickly, I recommend you check out...
    http://www.zend.com/manual/language.types.string.php
    and
    http://www.zend.com/manual/language.types.array.php
    and...
    any other pages there you might find interesting
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  14. #14
    SitePoint Addict mgkimsal's Avatar
    Join Date
    Sep 1999
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by samsm
    I think the official word from Zend (et al) is that you should abandon the use of double-quotes since the process to find variables in the quotes is quite costly. Not to mention it craps out quite soon (you can run benchmarks and it will eventually kill PHP).
    Funny, you'd think they'd want people to use " all the time so that there'd be more demand for the optimizer and encoder products.
    Michael Kimsal
    =============================
    groovymag.com - for groovy/grails developers
    jsmag.com - for javascript developers

  15. #15
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone for the info and the links. I bought "Sam's Teach yourself PHP in 24 hours" today so hopefully I won't have to ask so many questions now
    coiL
    "cradled in the learning curve"


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
  •