如何在php中使用三元运算符将值分配给多个变量?

时间:2019-04-12 07:38:02

标签: php codeigniter variables variable-assignment ternary-operator

我已经使用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);

我知道我可以为一个变量分配一个值,但是我想在三元表达式中分配多个变量。我该怎么办?

1 个答案:

答案 0 :(得分:0)

当我尝试并使用许多变通方法时,我发现了一个绝对可以按我想要的方法工作,如下所示:

<?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);

希望它将对某人有所帮助。