Learning PHP: Problem with Ternary Operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert

    #1

    Learning PHP: Problem with Ternary Operator

    Hi,

    I have a book that I've been using to learn PHP and MySQL.
    In it, it uses some code to show you how to use hidden fields which is not
    working correctly.

    The code I'm using is:
    *************** *************** *************** ********
    <?php
    // The number of guesses is either 0 or $num_tries + 1
    $num_tries = (isset($_POST[num_tries])) ? $num_tries + 1 : 0;
    // The number to guess
    $num_to_guess = 42;
    // The message to display
    $message = "";

    // Now we need to check the guess value
    // We start by seeing if there is any value in the guess value
    if (!isset($_POST[guess])){
    $message = "Welcome to the guessing Machine!";
    } elseif ($_POST[guess] > $num_to_guess ){
    $message = "$_POST[guess] is to big! Try a smaller number";
    } elseif ($_POST[guess] < $num_to_guess ) {
    $message = "$_POST[guess] is to small! Try a bigger number";
    } else {
    $message = "Well Done!";
    }
    // We are placing the Posted Guess value into a holder called $guess
    $guess = $_POST[guess];
    ?>
    <html>
    <head>
    <title>Listin g 9.8. Saving state with a hidden field</title>
    </head>

    <body>
    <h1><?php print $message ?></h1>
    Guess Number: <?php print $num_tries ?>
    <?php print "\n" ?>
    <form action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
    Type your guess here:
    <input type="text" name="guess" value="<?php print $guess ?>">
    <input type="hidden" name="num_tries " value="<?php print $num_tries ?>">
    </form>
    </body>
    </html>
    *************** *************** *************** *************** *****
    The problem I'm having is to do with the hidden value. It increments from 0
    to 1 fine,
    but then it won't increment it any further. It stays at 1.

    I have also tried replacing the code:
    "
    // The number of guesses is either 0 or $num_tries + 1
    $num_tries = (isset($_POST[num_tries])) ? $num_tries + 1 : 0;
    "
    with
    "
    // The number of guesses is either 0 or $num_tries + 1
    if (isset($_POST[num_tries])) {
    $num_tries = $num_tries + 1;
    } else {
    $num_tries = 0;
    }
    "

    with no luck either....
    Any suggestions?


  • Ian.H

    #2
    Re: Learning PHP: Problem with Ternary Operator

    On Tue, 14 Sep 2004 11:38:29 GMT, "Robert" <lonerngraus@ya hoo.com.au>
    wrote:
    [color=blue]
    >Hi,
    >
    >I have a book that I've been using to learn PHP and MySQL.
    >In it, it uses some code to show you how to use hidden fields which is not
    >working correctly.
    >
    >The code I'm using is:
    >************** *************** *************** *********
    ><?php
    >// The number of guesses is either 0 or $num_tries + 1
    >$num_tries = (isset($_POST[num_tries])) ? $num_tries + 1 : 0;
    >// The number to guess
    >$num_to_gues s = 42;[/color]


    [ snip ]

    [color=blue]
    ><input type="hidden" name="num_tries " value="<?php print $num_tries ?>">
    ></form>
    ></body>
    ></html>
    >************** *************** *************** *************** ******
    >The problem I'm having is to do with the hidden value. It increments from 0
    >to 1 fine,
    >but then it won't increment it any further. It stays at 1.
    >
    >I have also tried replacing the code:
    >"
    >// The number of guesses is either 0 or $num_tries + 1
    >$num_tries = (isset($_POST[num_tries])) ? $num_tries + 1 : 0;
    >"
    >with
    >"
    >// The number of guesses is either 0 or $num_tries + 1
    >if (isset($_POST[num_tries])) {
    > $num_tries = $num_tries + 1;
    >} else {
    > $num_tries = 0;
    >}
    >"
    >
    >with no luck either....
    >Any suggestions?
    >[/color]



    $num_tries = (isset($_POST['num_tries'])) ? $_POST['num_tries'] + 1 : 0;


    Reason:
    When the form's submitted, assuming 'num_tries' is submitted, your
    $num_tries var is _always_ undefined. You then fill it with the data
    depending on $_POST['num_tries']. _This_ is the value that needs the
    incrementation which is _then_ stored in your $num_tries var. Until that
    point, ignoring the fact $num_tries is undefined, consider it as '0'. 0
    + 1 will always be 1 =)


    HTH.



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK

    Comment

    • Kevin Thorpe

      #3
      Re: Learning PHP: Problem with Ternary Operator

      Robert wrote:[color=blue]
      > Hi,
      >
      > I have a book that I've been using to learn PHP and MySQL.
      > In it, it uses some code to show you how to use hidden fields which is not
      > working correctly.[/color]

      You need quotes round your field names.
      $_POST['num_tries'] not $_POST[num_tries]

      Comment

      • Ian.H

        #4
        Re: Learning PHP: Problem with Ternary Operator

        On 14 Sep 2004 04:56:43 -0700, "Steve" <googlespam@nas tysoft.com> wrote:
        [color=blue]
        >
        >
        >You (and the book) are probably assuming that Register Globals is on,
        >when it is off by default on all modern installations.
        >
        >So with the code as shown, if the posted (hidden) variable has a value
        >it is ignored and instead the uninitialised php variable $num_tries is
        >incremented from 0 to 1.
        >
        >The first line should read:
        >
        >$num_tries = (isset($_POST['num_tries'])) ? $_POST['num_tries'] + 1
        >: 0;
        >
        >
        >---
        >Steve[/color]


        He is using _POST.. even checking with isset().. but was trying to
        increment $num_tries before populating it with the value of
        $_POST['num_tries'] =)



        Regards,

        Ian

        --
        Ian.H
        digiServ Network
        London, UK

        Comment

        • Ian.H

          #5
          Re: Learning PHP: Problem with Ternary Operator

          On Tue, 14 Sep 2004 13:02:26 +0100, Kevin Thorpe <kevin@pricetra k.com>
          wrote:
          [color=blue]
          >Robert wrote:[color=green]
          >> Hi,
          >>
          >> I have a book that I've been using to learn PHP and MySQL.
          >> In it, it uses some code to show you how to use hidden fields which is not
          >> working correctly.[/color]
          >
          >You need quotes round your field names.
          >$_POST['num_tries'] not $_POST[num_tries][/color]


          Not the problem and isn't _required_ but is better coding practice and
          strongly advised.



          Regards,

          Ian

          --
          Ian.H
          digiServ Network
          London, UK

          Comment

          • rstuckart@stny.rr.com

            #6
            Re: Learning PHP: Problem with Ternary Operator

            On 14 Sep 2004 04:56:43 -0700, "Steve" <googlespam@nas tysoft.com>
            wrote:

            snip ...
            [color=blue]
            >The first line should read:
            >
            >$num_tries = (isset($_POST['num_tries'])) ? $_POST['num_tries'] + 1
            >: 0;
            >
            >
            >---
            >Steve[/color]

            This works too:
            $num_tries = isset($_POST['num_tries']) ? $_POST['num_tries'] + 1 : 0;

            a function such as isset or empty does not have to be enclosed in
            parentheses

            Bob

            Comment

            Working...