3

I am having an issue - I am unable to get a td cell when my $v value is 0. For all other numbers it is fine, but with zero value I just don't get then td.. What could be wrong? I am using $_GET array with a foreach loop, taking data from two inputs, one for name, one for value.

<table border = "1" >
<?php 
print_r($_GET); 
foreach ($_GET as $k=>$v){
    if($v){
        if ($rowFinished) echo "<tr>";

        if (preg_match("/{$inputVardas}/i", $k))
        {
           echo "<td>$v</td>";
           $rowFinished = false;
        }
        else if (preg_match("/{$inputSkaicius}/i", $k) and is_numeric($v))
        {
            if ($v < 10)
            {
                $color="green";
            }
            else if ($v > 10)
            {
                $color="red";
            }
            else if( $v == 10){
                $color="yellow";
            }

            echo "<td style='color: $color'>$v</td>";
            $rowFinished = true;
        }
        if ($rowFinished) echo "</tr>";
    }
}?></table>

I've tried to do some print_r of an array, there I can see a value, but in my loop it just doesn't work. Maybe it has to do something with the case that php treats it as empty, but I am unable to find a way to use it.

 [vardas0] => jonas [value0] => 0 
andriusjon
  • 43
  • 3
  • +1 fpr using print_r() to verify the contents. SUGGESTIONS: 1) Verify `if($v)` is true, 2) Check `$rowFinished`. 3) Check both `preg_match()` – FoggyDay Jan 09 '20 at 19:19
  • I used print_r, and the result with zero is like I've shown in my question.. It is zero. Could you be more specific with your suggestion? – andriusjon Jan 09 '20 at 19:23
  • 3
    Well, by doing `if($v)` and if `$v` is a zero (string or int, doesnt matter), then that `if` block will be skipped, since zero is falsey. – IncredibleHat Jan 09 '20 at 19:24
  • You may be better off creating arrays of values from your input on the HTML side - https://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array. – Nigel Ren Jan 09 '20 at 19:29
  • Based on this: `[vardas0] => jonas [value0] => 0 `, I didn't get that `_GET[0]` was the value "0" ... which is equivalent to *FALSE*. Which was actually the whole problem. Doh! Glad you figured it out ... and glad you figured out a way to distinguish "false" from the digit "0" ;) – FoggyDay Jan 09 '20 at 22:11

4 Answers4

2

Directly after the foreach you have a if ($v), and if the value is 0, it will not run.

MultiSuperFreek
  • 417
  • 3
  • 8
0

I've found a way by improving the following line from

if($v){

to

if($v or (is_numeric($v) and $v == 0)){
andriusjon
  • 43
  • 3
0

It looks like you're using this

if ($v) {

to check that $v has a value. You can use

if (strlen($v)) {

instead.

Everything in $_GET will be a string, but a string '0' will evaluate as false just like an int 0 as you have discovered. Checking the length of the string instead will tell you whether or not that URL parameter has any value, including '0'.

You may also want to consider trimming the value before checking it if you want to exclude values containing only whitespace.

$v = trim($v);
if (strlen($v)) {
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

I honestly would suggest using isset()instead of all the provided suggestions so far. The reason is that to me it is best practice to have your conditionals be a boolean expression or something that explicitly evaluates to true or false. This avoids ambiguity and unintended side effects as evidenced by this question.

I would personally use:

if(isset($v)){
Rami
  • 67
  • 5