# IF clause with precondition

- Thread: https://externals.io/message/48163
- Original archive: https://news-web.php.net/php.internals/48163
- Started by soenke.brecht@web.de on 2010-04-29T18:08:39+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #48163 — soenke.brecht@web.de — 2010-04-29T18:08:39+00:00

URL: https://externals.io/message/48163#48163

Following code snippet did raise warnings concerning missing single 
array values ($buffer[$SourceColumnNo] is `NULL` for some keys)

if(is_numeric($SourceColumnNo = 	
	array_search($ColumnName, $SourceColumns[$SourceFileName])))
{
/*
  * The table column name was found in the current source file
  * now store it with the column order number of the table at the correct
  * position of the ValueArray which will later be transferred to the
  * table. Sometimes the decimal seperator is given as comma "," instead
  * of point "." this is adjusted as well.
  */
	$ValueArray[$ColumnNo] = str_replace(",", ".",
			trim($buffer[$SourceColumnNo]));
}

My solution was to wrap around if(isset($buffer[$SourceColumnNo])){...}

Is there a way to get this additional if condition into the code snippet 
without evaluating the warning raising second part?

Putting both conditions into the clause still causes both conditions to 
be evaluated, regardles if the full clause maybe never true because 
 `FALSE` AND sth. will always be FALSE.
if(isset($buffer[$SourceColumnNo]) AND is_numeric($SourceColumnNo = 	
	array_search($ColumnName, $SourceColumns[$SourceFileName]))){...};

Is for instance this evaluation behaviour controllable?

Error suppression with @ is not acceptable and try / catch with error to 
exception conversion is as well causing code overhead and reduces 
readability.

As third option is to use ternary operator:
if(isset($buffer[$SourceColumnNo]) ? (is_numeric($SourceColumnNo = 	
	array_search($ColumnName, $SourceColumns[$SourceFileName]))) : FALSE){};

Is this real good practise? Is this code still readable and does it 
still meet common maintainability requirements?

Thanks for your help.

---

## #48164 — Ferenc Kovacs — 2010-04-29T18:54:24+00:00

URL: https://externals.io/message/48163#48164 · In reply to: #48163

> Following code snippet did raise warnings concerning missing single array
> values ($buffer[$SourceColumnNo] is `NULL` for some keys)
>
>
if(!empty($SourceColumns[$SourceFileName]) && is_numeric($SourceColumnNo =
array_search($ColumnName, $SourceColumns[$SourceFileName])))
{

but I would split it into multiple checks.

Tyrael
