user6104636 user6104636 - 7 months ago 14
PHP Question

How to skip lines in php

I'm struggling in finding a way to correctly do this logic.

If (this thing is null)
Skip it
Else
Don't skip it


I tried with if/else and while loops but each one will crash the program. I test something like this:

(inside a foreach)
if($value->getThing() == NULL) {
//HOW TO SKIP???
//I try to 'set' this thing
$value->setThing(0); //BUT IT Doesn't work because it's an associated object...
} else {
$value->getThing();
}


And tried this:

(inside foreach)
while ($value->getThing() != NULL) {
$value->getThing();
//Do Calculation...
}


Both just crash when it gets to the thing thats null. I know why but I can't figure out how to skip the null thing.

and if you can't tell, I'm a newbie. But I'm learning.

EDIT: The thing is null in the db.

Answer

Try this code :

foreach($values as $value){
   if(!is_null($value->getThing())){
    #do calculation
  }
}