如何在 php 中使用三元运算符为多个变量赋值?
How to assign values to multiple variables using ternary operator in php?
我已经使用 if 语句在 codeigniter 中完成了以下代码来实现我的逻辑,但我想使用三元运算符来完成它:
<?php
$payment_via = $this->input->post('payment');
if($payment_via == "bank"){
$this->page_data['message_type'] = "paid_via_bank";
$this->page_data['message'] = "You have Successfully Subscribed plan via Bank cheque.";
}else{
$this->page_data['message_type'] = "paid_via_online";
$this->page_data['message'] = "You have Successfully Subscribed plan via Online payment.";
}
$this->load->view('payment/checkout_msg', $this->page_data);
我知道我可以为单个变量赋值,但是我想在三元表达式中为多个变量赋值。我该怎么做?
当我尝试并使用许多变通方法时,我发现了一个完全符合我要求的方法,如下所示:
<?php
$payment_via = $this->input->post('payment');
($payment_via == "bank") ? (($this->page_data['message_type'] = "paid_via_bank") && ($$this->page_data['message'] = "You have Successfully Subscribed plan via Bank cheque.")) : (($this->page_data['message_type'] = "paid_via_online") && ( $this->page_data['message'] = 'You have Successfully Subscribed plan via Online payment.'));
$this->load->view('demo/checkout_msg', $this->page_data);
希望对大家有所帮助。
我已经使用 if 语句在 codeigniter 中完成了以下代码来实现我的逻辑,但我想使用三元运算符来完成它:
<?php
$payment_via = $this->input->post('payment');
if($payment_via == "bank"){
$this->page_data['message_type'] = "paid_via_bank";
$this->page_data['message'] = "You have Successfully Subscribed plan via Bank cheque.";
}else{
$this->page_data['message_type'] = "paid_via_online";
$this->page_data['message'] = "You have Successfully Subscribed plan via Online payment.";
}
$this->load->view('payment/checkout_msg', $this->page_data);
我知道我可以为单个变量赋值,但是我想在三元表达式中为多个变量赋值。我该怎么做?
当我尝试并使用许多变通方法时,我发现了一个完全符合我要求的方法,如下所示:
<?php
$payment_via = $this->input->post('payment');
($payment_via == "bank") ? (($this->page_data['message_type'] = "paid_via_bank") && ($$this->page_data['message'] = "You have Successfully Subscribed plan via Bank cheque.")) : (($this->page_data['message_type'] = "paid_via_online") && ( $this->page_data['message'] = 'You have Successfully Subscribed plan via Online payment.'));
$this->load->view('demo/checkout_msg', $this->page_data);
希望对大家有所帮助。