473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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>Listing 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?
Jul 17 '05 #1
5 3554
On Tue, 14 Sep 2004 11:38:29 GMT, "Robert" <lo*********@yahoo.com.au>
wrote:
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;

[ snip ]

<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?


$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
http://digiserv.net/
Jul 17 '05 #2
Robert wrote:
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.


You need quotes round your field names.
$_POST['num_tries'] not $_POST[num_tries]
Jul 17 '05 #3
On 14 Sep 2004 04:56:43 -0700, "Steve" <go********@nastysoft.com> wrote:


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

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
http://digiserv.net/
Jul 17 '05 #4
On Tue, 14 Sep 2004 13:02:26 +0100, Kevin Thorpe <ke***@pricetrak.com>
wrote:
Robert wrote:
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.


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

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

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #5
On 14 Sep 2004 04:56:43 -0700, "Steve" <go********@nastysoft.com>
wrote:

snip ...
The first line should read:

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


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
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
10
by: Barry Morris | last post by:
Hi I am a php newbie although I have been a programmer for years, this can be dangerous because all the languages I know use = as equal comparison so it took me a long time to debug if ($STRING...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
3
by: Luigi | last post by:
Hi to all, I' d like to know if exists some sentences like this one below (in java) for PHP: JAVA: boolean isNull = (myObj == null) ? true : false; PHP: $isNull = ???????????????;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.