as the title indicates, I am currently using a strstr to trim strings like this
$destino = "Piura - Lima";
echo strstr($destino, ' - ', true)
Result: Piura
But if it doesn’t find the value the strstr returns a null value
Example
$destino = "Piura * Lima";
echo strstr($destino, ' - ', true)
Result: NULL
Is there any parameter/option to return the value of $destino if it doesn’t find the match?
Maybe something similar that will do the result?
Anything goes but I prefer to use strstr.
Note: I know this can be fixed with a simple IF but I want as minimal as possible.
>Solution :
You can use the empty ternary operator (also sometimes called the Elvis operator) to default back to your original value in this case:
$destino = "Piura * Lima";
$value = strstr($destino, ' - ', true) ?: $destino;
var_dump($value);
Output:
string(12) "Piura * Lima"
This is basically the same as:
$value = strstr($destino, ' - ', true);
if ($value === false) {
$value = $destino;
}
Note that strstr() returns FALSE here, and not NULL.