473,394 Members | 1,932 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,394 software developers and data experts.

Whats wrong with this code?

$antroj=0;
$dtctxsz1 = $_POST["dtctxsz1"];
if($dtctxsz1 == 0){
print $dtctxsz1;
$antroj = $antroj + 1;
}

It adds 1 to $antroj even when the value of $dtctxsz1 is not 0. I have
spent 7 hours on this one problem. Its driving me nuts.

Garry Jones
Sweden
Oct 5 '06 #1
7 1131
Off the top of my head, since you don't say what $dtctxsz1 is. I'd say
its because $dtctxsz1 is blank or null, which "== 0" returns true for.

you could try $dtctxsz1 === '' or !isset($dtctxsz1).

Gavin

On Oct 5, 12:09 pm, "Garry Jones" <garry.jo...@morack.sewrote:
$antroj=0;
$dtctxsz1 = $_POST["dtctxsz1"];
if($dtctxsz1 == 0){
print $dtctxsz1;
$antroj = $antroj + 1;

}It adds 1 to $antroj even when the value of $dtctxsz1 is not 0. I have
spent 7 hours on this one problem. Its driving me nuts.

Garry Jones
Sweden
Oct 5 '06 #2
<ha*****@gmail.comskrev i meddelandet
news:11**********************@e3g2000cwe.googlegro ups.com...
Off the top of my head, since you don't say what $dtctxsz1 is. I'd say
its because $dtctxsz1 is blank or null, which "== 0" returns true for.
Here is some more of the offending code.

First - Exerpt from a form.

<select name="dtctxsz1" class="vtredstar" id="dtctxsz1">
<option value="0" selected>Ej vald</option>
<option value="xs">XS</option>
<option value="s">S</option>
<option value="m">M</option>
<option value="l" >L</option>
<option value="xl">XL</option>
<option value="xxl">XXL</option>
<option value="xxxl">XXXL</option>
</select>

Processed by.....

$antroj = 0;
$dtctxsz1 = $_POST["dtctxsz1"];
if($dtctxsz1 == 0){
print $dtctxsz1;
$antroj = $antroj + 1;
}

If the user chooses xl the value of $dtctxsz1 is xl.

It should not then execute the print command because $dtctxsz1 is not 0.

However it prints, and it prints the value the user clicked on - in this
case xl

It should not be adding one to the value of $antroj should it. But it is!

If the value was misfiring it would be printing 0 because it can only print
when the value is 0. So I am missing something here!

Greatful for any help in this matter.

Garry Jones
Sweden
Oct 5 '06 #3
Garry Jones wrote:
$antroj=0;
$dtctxsz1 = $_POST["dtctxsz1"];
if($dtctxsz1 == 0){
print $dtctxsz1;
$antroj = $antroj + 1;
}

It adds 1 to $antroj even when the value of $dtctxsz1 is not 0. I have
spent 7 hours on this one problem. Its driving me nuts.
It works for me. Maybe some of the code you didn't post is the culprit.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

$antroj = 0;
$dtctxsz1 = '14'; // $_POST["dtctxsz1"];
if ($dtctxsz1 == 0) {
// print $dtctxsz1;
++$antroj;
}
echo 'after \'14\' $antroj is ', $antroj, ".\n<br>";

$dtctxsz1 = '0'; // $_POST["dtctxsz1"];
if ($dtctxsz1 == 0) {
// print $dtctxsz1;
++$antroj;
}
echo 'after \'0\' antroj is ', $antroj, ".\n<br>";

$dtctxsz1 = 'test'; // $_POST["dtctxsz1"];
if ($dtctxsz1 == 0) {
// print $dtctxsz1;
++$antroj;
}
echo 'after \'test\' $antroj is ', $antroj, ".\n<br>";

?>

Oct 5 '06 #4
Pedro Graca wrote:
It works for me. Maybe some of the code you didn't post is the culprit.
I agree. Or else (this is a long shot) maybe you accidentally used a
numeral one "1" (instead of lower case L) in the variable name in the if
statement, being undefined it's somehow equating to zero?
Oct 5 '06 #5
Garry Jones wrote:
First - Exerpt from a form.

<select name="dtctxsz1" class="vtredstar" id="dtctxsz1">
<option value="0" selected>Ej vald</option>
<option value="xs">XS</option>
[...]
</select>

Processed by.....

$antroj = 0;
$dtctxsz1 = $_POST["dtctxsz1"];
if($dtctxsz1 == 0){
print $dtctxsz1;
$antroj = $antroj + 1;
}
if ('fortytwo' == 0) echo 'Douglas Adams had it wrong!';

You should have a look at the Type Juggling manual page:
http://www.php.net/manual/en/languag...e-juggling.php
<?php
header('Content-Type: text/plain');
error_reporting(E_ALL);
ini_set('display_errors', '1');

$x1 = '0';
$x2 = 'xs';
$x3 = 0;

$tests = array();
$tests[] = 'return $x1 == $x2;';
$tests[] = 'return $x1 === $x2;';
$tests[] = 'return $x1 == $x3;';
$tests[] = 'return $x1 === $x3;';
$tests[] = 'return $x2 == $x3;';
$tests[] = 'return $x2 === $x3;';
$tests[] = 'return $x1 == 0;';
$tests[] = 'return $x1 === 0;';
$tests[] = 'return $x1 == \'0\';';
$tests[] = 'return $x1 === \'0\';';
$tests[] = 'return $x2 == 0;';
$tests[] = 'return $x2 === 0;';
$tests[] = 'return $x2 == \'0\';';
$tests[] = 'return $x2 === \'0\';';
$tests[] = 'return $x3 == 0;';
$tests[] = 'return $x3 === 0;';
$tests[] = 'return $x3 == \'0\';';
$tests[] = 'return $x3 === \'0\';';

echo '$x1 = \'0\';
$x2 = \'xs\';
$x3 = 0;

';
$n = 0;
foreach ($tests as $v) {
echo $v, "\treturns ", (eval($v)) ? ('true') : ('false'), "\n";
}
?>

--
File not found: (R)esume, (R)etry, (R)erun, (R)eboot
Oct 5 '06 #6
On Thu, 5 Oct 2006 22:06:57 +0200, in comp.lang.php "Garry Jones"
<ga*********@morack.se>
<eg**********@yggdrasil.glocalnet.netwrote:
>| <ha*****@gmail.comskrev i meddelandet
| news:11**********************@e3g2000cwe.googlegro ups.com...
| Off the top of my head, since you don't say what $dtctxsz1 is. I'd say
| its because $dtctxsz1 is blank or null, which "== 0" returns true for.
|
| Here is some more of the offending code.
|
| First - Exerpt from a form.
|
| <select name="dtctxsz1" class="vtredstar" id="dtctxsz1">
| <option value="0" selected>Ej vald</option>
| <option value="xs">XS</option>
| <option value="s">S</option>
| <option value="m">M</option>
| <option value="l" >L</option>
| <option value="xl">XL</option>
| <option value="xxl">XXL</option>
| <option value="xxxl">XXXL</option>
| </select>
|
| Processed by.....
|
| $antroj = 0;
| $dtctxsz1 = $_POST["dtctxsz1"];
| if($dtctxsz1 == 0){
| print $dtctxsz1;
| $antroj = $antroj + 1;
| }
The select control returns a string value so you should change your
code to:
if($dtctxsz1 == "0"){
>| If the user chooses xl the value of $dtctxsz1 is xl.
|
| It should not then execute the print command because $dtctxsz1 is not 0.
|
| However it prints, and it prints the value the user clicked on - in this
| case xl
|
| It should not be adding one to the value of $antroj should it. But it is!
|
| If the value was misfiring it would be printing 0 because it can only print
| when the value is 0. So I am missing something here!
|
| Greatful for any help in this matter.
|
| Garry Jones
| Sweden
|
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Oct 5 '06 #7
Gary Hasler" <ga********@thelogconnection.comskrev i meddelandet
news:45***************@thelogconnection.com...
I agree. Or else (this is a long shot) maybe you accidentally used a
numeral one "1" (instead of lower case L) in the variable name in the if
statement, being undefined it's somehow equating to zero?
Thanks to all.

Jeff hit the nail on the head....

The select control returns a string value so you should change your
code to:
if($dtctxsz1 == "0"){

Amazing what a pair of quotation marks can do.

Garry Jones
Sweden
Oct 6 '06 #8

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

Similar topics

6
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item)...
3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
1
by: aa | last post by:
When I am reading from local disk (d:), everithing is OK, but then I am reading from map disk I am geting the this error. Whats wrong. Thanks Server Error in '/Extra' Application....
3
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');"...
4
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the...
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
1
by: Abubakar | last post by:
Hi, In my application, Some of my thread gets stuck somewhere. The vs2k5 debugging "Thread" window shows that stuck thread and I can I dentify it. In its Name column the following text is...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
5
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.