Mapping Values From Your File with IF Statements

Mapping Values From Your File with IF Statements

As WP All Import allows you to use PHP functions on your data, you can create your own custom PHP functions that can be passed values from your data file, and return a certain result based on if/else conditions you set in your function.

Example A – If an element is empty

In this example, we』re checking to see if the element passed is empty or not, and returning a simple text string.

function is_element_empty($element) {
if(empty($element)) {
return "This element is empty.";
} else {
return "This element is not empty!";
}
}

Example B – If an element contains a value

In this example, we』re passing two parameters: the element from the data file that we want to check, and the value we want to search this element for.

function my_contains_function($element, $value_to_search_for) {
if (stripos($element, $value_to_search_for) !== false) {
return $element;
} else {
return;
}
}

If the element passed to this function contains the value to search for, then the element will be returned. Otherwise, an empty result will be returned.

Example C – If one element contains a greater value than another element

In this example, we』re passing two parameters again, this time in the context of comparing two elements that contain prices.

function choose_price_to_return($price1, $price2) {
if ($price1 > $price2) {
return $price1;
} else {
return $price2;
}
}

If the first element passed has a greater value than the second element, the first element will be returned. Otherwise, the second element will be returned.

Further reading:

if statements: http://php.net/manual/en/control-structures.if.phpelse statements: http://php.net/manual/en/control-structures.else.phpelseif statements: http://php.net/manual/en/control-structures.elseif.php

Note: If you』re needing some inspiration for writing your own custom IF/ELSE functions, a good resource to check out is stackoverflow.com, as there are lots of code discussions there.

Related

Inline PHP

FOREACH Loops

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注