简体   繁体   English

如何在php中使用三元运算符为多个变量赋值?

[英]How to assign values to multiple variables using ternary operator in php?

I have done following code in codeigniter using if statement for fulfilling my logic, but I want to do it using a 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);

I know that I can assign a value to a single variable, but I want to assign multiple variables in a ternary expression .我知道我可以为单个变量赋值,但我想在三元表达式中分配多个变量 How can I do it?我该怎么做?

When i was trying and playing with many workarounds i have found one which works absolutely as i want which is as below:当我尝试并尝试使用许多解决方法时,我发现了一种绝对可以按我想要的方式工作的方法,如下所示:

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

Hope it will help someone.希望它会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM