comparing strings - again!
6 answers - 872 bytes -

This does not work although when I echo out the strings they are exactly the
same. Strange!
"Ross" <ross (AT) aztechost (DOT) comwrote in message news:
if (isset($_PST['Submit'])) {
//echo "post equals".$_PST['x']." corect
is".$correct_answers[$page-1];
$compare1 = $_PST['x'];
echo "page is".$page;
$compare2 = $correct_answers[($page-1)];
echo "compare1 is ".$compare1;
echo "<BR>";
echo "compare2 is ".$compare2;
if (strcmp($compare1, $compare2) == 0) {
$incrementedPage = $page + 1;
?>
<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//
</script>
<?
//header("Location: evaluation.php?page=$incrementedPage");
}
else {
$chances++;
echo "you have had".$chances;
}
}
--
?>
No.1 | | 1633 bytes |
| 
var_dump gives
Company Director string(17)
Company Director string(16)
Why would they be different? Seems like they have add some extra whitespace?
R.
Message
From: "Jochem Maas" <jochem (AT) iamjochem (DOT) com>
To: "Ross" <ross (AT) aztechost (DOT) com>
Cc: <php-general (AT) lists (DOT) php.net>
Sent: Tuesday, June 20, 2006 3:57 PM
Subject: Re: [PHP] Re: comparing strings - again!
Ross wrote:
>This does not work although when I echo out the strings they are exactly
>the
>same. Strange!
>
try using var_dump($compare1, $compare2);
>
>>
>>
>"Ross" <ross (AT) aztechost (DOT) comwrote in message news:
if (isset($_PST['Submit'])) {
//echo "post equals".$_PST['x']." corect
is".$correct_answers[$page-1];
$compare1 = $_PST['x'];
echo "page is".$page;
$compare2 = $correct_answers[($page-1)];
echo "compare1 is ".$compare1;
echo "<BR>";
echo "compare2 is ".$compare2;
if (strcmp($compare1, $compare2) == 0) {
$incrementedPage = $page + 1;
?>
<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//
</script>
<?
//header("Location: evaluation.php?page=$incrementedPage");
}
else {
$chances++;
echo "you have had".$chances;
}
}
?>
>>
>
>
No.2 | | 1639 bytes |
| 
Soted it was an extra whitespace character being added to the posted answer.
The var_dump function highlighted it! nice Work Jochem. No idea where it
campe from though!
R.
Message
From: "Jochem Maas" <jochem (AT) iamjochem (DOT) com>
To: "Ross" <ross (AT) aztechost (DOT) com>
Cc: <php-general (AT) lists (DOT) php.net>
Sent: Tuesday, June 20, 2006 3:57 PM
Subject: Re: [PHP] Re: comparing strings - again!
Ross wrote:
>This does not work although when I echo out the strings they are exactly
>the
>same. Strange!
>
try using var_dump($compare1, $compare2);
>
>>
>>
>"Ross" <ross (AT) aztechost (DOT) comwrote in message news:
if (isset($_PST['Submit'])) {
//echo "post equals".$_PST['x']." corect
is".$correct_answers[$page-1];
$compare1 = $_PST['x'];
echo "page is".$page;
$compare2 = $correct_answers[($page-1)];
echo "compare1 is ".$compare1;
echo "<BR>";
echo "compare2 is ".$compare2;
if (strcmp($compare1, $compare2) == 0) {
$incrementedPage = $page + 1;
?>
<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//
</script>
<?
//header("Location: evaluation.php?page=$incrementedPage");
}
else {
$chances++;
echo "you have had".$chances;
}
}
?>
>>
>
>
No.3 | | 908 bytes |
| 
ross (AT) aztechost (DOT) com wrote:
var_dump gives
Company Director string(17)
Company Director string(16)
Why would they be different?
probably because there is either:
1. white space in the value in your data source
2. white space being outputted along side the value when creating a form field weith that valu
that and/or possible the fact that your doing a trim()
in one place but not in another - personally I usually trim() incoming string data
(regardless of any other validation/sanitation) to avoid stuff like this
Seems like they have add some extra
whitespace?
exactly. btw: var_dump() should be showing exactly where the white space is e.g.:
code:
<?php
$s1 = "Company Director "; $s2 = "Company Director"; var_dump($s1, $s2);
output:
string(17) "Company Director "
string(16) "Company Director"
No.4 | | 1255 bytes |
| 
Ross wrote:
This does not work although when I echo out the strings they are exactly the
same. Strange!
try using var_dump($compare1, $compare2);
"Ross" <ross (AT) aztechost (DOT) comwrote in message news:
>if (isset($_PST['Submit'])) {
>//echo "post equals".$_PST['x']." corect
>is".$correct_answers[$page-1];
>$compare1 = $_PST['x'];
>echo "page is".$page;
>$compare2 = $correct_answers[($page-1)];
>echo "compare1 is ".$compare1;
>echo "<BR>";
>echo "compare2 is ".$compare2;
>>
>if (strcmp($compare1, $compare2) == 0) {
>$incrementedPage = $page + 1;
>?>
><script type="text/javascript">
><!--
>window.location = "http://www.google.com/"
>//
></script>
><?
>//header("Location: evaluation.php?page=$incrementedPage");
>}
>>
>else {
>$chances++;
>echo "you have had".$chances;
>}
>>
>}
>>
>>
>?>
>>
No.5 | | 1403 bytes |
| 
Jochem Maas wrote:
ross (AT) aztechost (DOT) com wrote:
>var_dump gives
>>
>Company Director string(17)
>>
>Company Director string(16)
>>
>Why would they be different?
probably because there is either:
1. white space in the value in your data source
2. white space being outputted along side the value when creating a form field weith that valu
that and/or possible the fact that your doing a trim()
in one place but not in another - personally I usually trim() incoming string data
(regardless of any other validation/sanitation) to avoid stuff like this
>Seems like they have add some extra
>whitespace?
exactly. btw: var_dump() should be showing exactly where the white space is e.g.:
code:
<?php
$s1 = "Company Director "; $s2 = "Company Director"; var_dump($s1, $s2);
output:
string(17) "Company Director "
string(16) "Company Director"
Without the quotes he's not going to see the extra white space though,
as the browser is going to render the first space it comes too, but it
will piss on any white space after that (for display purpose). Main
reason I wrap my debugging data in <pre></pre>.
No.6 | | 1058 bytes |
| 
John Nichel wrote:
Jochem Maas wrote:
>>
>exactly. btw: var_dump() should be showing exactly where the white
>space is e.g.:
>>
>>
>code:
><?php
>$s1 = "Company Director "; $s2 = "Company Director"; var_dump($s1, $s2);
>>
>output:
>string(17) "Company Director "
>string(16) "Company Director"
>>
Without the quotes he's not going to see the extra white space though,
as the browser is going to render the first space it comes too, but it
will piss on any white space after that (for display purpose). Main
reason I wrap my debugging data in <pre></pre>.
I use <pretoo when debugging in the browser - but I was testing on the
cmdline, that said var_dump() outputs the quotes when outputting to the browser
too. I wasn't 100% sure but I just checked to be safe ;-)