Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Walker's avatar

PHP logical operators

Hi, how could a condition be done if clientref is null displays only orderId without a hyphen. well thank you

'ClientReference' => $this->options['clientref'] .'-'. $this->filterOrderId($order)
0 likes
8 replies
VinayPrajapati's avatar

'ClientReference' => ($this->options['clientref'] ? $this->options['clientref'] .'-'. : '').$this->filterOrderId($order)

Use this.

MostafaGamal's avatar

Try this:


        'ClientReference' => is_null($this->options['clientref']) ?
                                    $this->filterOrderId($order) :
                                    $this->options['clientref'] . '-' . $this->filterOrderId($order);
Walker's avatar

But it doesn't work. It didn't remove the hyphen from me

MostafaGamal's avatar

the hyphen will be removed only if $this->options['clientref'] is null !!

that what I thought you want when I read your question

tykus's avatar
tykus
Best Answer
Level 104

Use this:

'ClientReference' => implode(' - ', array_filter([$this->options['clientref'], $this->filterOrderId($order)]));

Whenever either $this->options['clientref'] or $this->filterOrderId($order) is falsey, they will be removed from the array, so implode will not have 2 items to put the hyphen between.

Walker's avatar

Thank you all, I'm sorry if I spoke badly. I have bad English.

Please or to participate in this conversation.