SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a moment when one wants to put quotes (' or ") around numbers in php?

    Hi!

    Is there a moment when one wants to put quotes (' or ") around numbers in php?

    Fort example:

    if ($_get["ssd"] == "0")


    or should I put it?

    if ($_get["ssd"] == 0)


    Why do I want to put quotes around the 0?? Well, simply cause as you all already know, $_get vars are always strings!!

    So in fact if $_get["ssd"] is empty, if ($_get["ssd"] == 0), will be true!! Problem here!!

    Thank you!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try:

    if(!isset($_GET["ssd"]))

    Why do I want to put quotes around the 0??
    To see if someone entered 0 in a text field rather than left it empty?

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that wasn't my question

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aqw
    that wasn't my question
    You haven't asked any question.

    == "0" and == 0 are both wrong unless you want numeric comparison. To compare strings, use either === or strcmp.

  5. #5
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You do not need to put numbers in quotes, at any time in php.. and $_GET vars aren't always strings...what if you get an integer?

  6. #6
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Waffles
    You do not need to put numbers in quotes, at any time in php.. and $_GET vars aren't always strings...what if you get an integer?
    are you sure about that?

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You do not need to put numbers in quotes, at any time in php.. and $_GET vars aren't always strings...what if you get an integer?
    Try this:
    PHP Code:
    echo gettype($_POST['age']); 

  8. #8
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tested $_get ... always a string!!!

  9. #9
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    echo '<a href="page.php?id=1">Link</a>';

    if(isset(
    $_GET['id'])) {

    echo 
    $_GET['id'];
    // echos 1
    }

    ?>
    there..integer.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Waffles
    PHP Code:
    <?
    echo '<a href="page.php?id=1">Link</a>';

    if(isset(
    $_GET['id'])) {

    echo 
    $_GET['id'];
    // echos 1
    }

    ?>
    there..integer.
    PHP Code:
    $str "1";
    echo 
    $str//echos 1 
    there..string

  11. #11
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    test with gettype!!!

    the 1 it echos is a string boy!!!

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aqw
    test with gettype!!!

    the 1 it echos is a string boy!!!

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP is loosely typed.. it'll cast between types for you to try to do whatever operation or comparison you want.

    Functions like isset(), is_numeric(), ctype_alnum() allow you to validate input is in the format you want it.

    is_numeric($_GET['id']) would return true

  14. #14
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    eez
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry if I sounded rude

  15. #15
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    They're both strings... but this is PHP, not Java, so it doesn't matter one bit.

    http://www.zend.com/.../language.types.type-juggling.php

  16. #16
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman
    They're both strings... but this is PHP, not Java, so it doesn't matter one bit.

    http://www.zend.com/.../language.types.type-juggling.php
    well true, types don't matter MUCH in PHP, but they do matter to some degree. since both strings and numbers are automatically evaluated to true/false...if you don't watch out, you may not get the expected result...though technically no error will occur.

    @agw: in your example used in your first post, quotes is the way to go, since anything obtained from the query string is a string (hence the name query string), it's always a good practice to compare it to the same type (instead of letting PHP interpret the type for you...which could lead to unexpected results), to ensure you get the results you want. technically it would be safer to convert it to an int first, but whether you do that before or after the if statement is up to you...its not required, and i don't feel its necessary.


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
  •