Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What is wrong with this code

    When this code is run the output is not what I expect.

    PHP Code:
    <?

    echo ( $id = (int) $_GET['id'] ? "[id{$id}]" '' ) . ', ';
    echo ( 
    $id = (int) $_GET['id'] ? "?id={$id}" '' );

    ?>

    The output I want is:

    [id12], ?id=12


    But the output I get is:

    [id12], ?id=[id12]


    Can anyone tell what is wrong with the code? Is there something incorrect about the order of operations in the conditional expressions?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    well

    PHP Code:
    <?
    echo ( $id = (int) $_GET['id'] ? "[id{$_GET['id']}]" '' ) . ', ';
    echo ( 
    $id = (int) $_GET['id'] ? "?id={$_GET['id']}" '' );
    ?>
    gives the answer you are looking for ... as does

    PHP Code:
    <?
    echo ((int) $_GET['id'] ? "[id{$id}]" '' ) . ', ';
    echo ((int) 
    $_GET['id'] ? "?id={$id}" '' );
    ?>
    lol actually with register_globals=off the second one does not


    but not having a clue what you are doing I dont know if that helps
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what is (int) for? are u trying check if $id is a number not?
    did u mean intval?
    if so, probably this how it should be?
    PHP Code:
    <?
    echo ( ($id intval($_GET['id'])) ? "[id{$_GET['id']}]" '' ) . ', ';
    echo ( (
    $id intval($_GET['id'])) ? "?id={$_GET['id']}" '' );
    ?>

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    (int) reduces the value to an integer, so if the value was a string, the value would become 0, thus making the conditional expression false, and nothing would happen. It is a way of validating the incoming data.

    Firepages, yes I can get some of your suggestions to work, kind of a combination of the two. Thanks.

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by Dylan Leblanc
    (int) reduces the value to an integer
    The proper terminology is "typecasting" for us programmer geeks.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Spookster
    The proper terminology is "typecasting" for us programmer geeks.
    hmmm...and why it's not published as a function at www.php.net ?
    or it's not actualy function?

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    geek ! - speak for yourself Spooks - lol & I just been told I am a scripter not a programmer the shame ..... anyway

    ok, also look at the '===' operator , it checks for type as well...

    PHP Code:
    <?
    $wha
    ='1';
    //$wha=1;
    echo ((int)$wha=== $wha)?'true':'false';
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    V@no , I dont think its technically a function as such , ? I dunno - anyway its just like typecasting in C which PHP is made from so it all makes sense eventually ~ check out
    http://www.php.net/manual/en/languag...e-juggling.php
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by firepages
    geek ! - speak for yourself Spooks - lol & I just been told I am a scripter not a programmer the shame ..... anyway

    ok, also look at the '===' operator , it checks for type as well...

    PHP Code:
    <?
    $wha
    ='1';
    //$wha=1;
    echo ((int)$wha=== $wha)?'true':'false';
    ?>
    you are a GID.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    GID ....

    scared to ask
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #11
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by firepages
    GID ....

    scared to ask
    I was waiting for you to ask. lol

    GID(Geek in Denial)
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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