Welcome Guest, Not a member yet? Register   Sign In
how to use if condition in array variables?
#1

How to use if condition in array
Code:
$insert = array();
       $insert[] = array(
           'appno' => $appno,
           'regno' => $regno,
          'certid' => $certid,
             'amt' => if($certid=1,8500 else 300 ,
             'date' => $today
               );      
Reply
#2
Information 

PHP Code:
'amt' => $certid==8500 300 
Reply
#3

(04-24-2019, 10:58 PM)kvanaraj Wrote: How to use if condition in array
Code:
$insert = array();
       $insert[] = array(
        'appno' => $appno,
        'regno' => $regno,
          'certid' => $certid,
             'amt' => if($certid=1,8500 else 300 ,
             'date' => $today
               );      

The key point that @Wouter60 makes (but does not explain) is that you assign a value of 1 to $certid, e.g. $certid=1. But what you really wanted to do was test for equality, e.g. $certid==1. What you should have written was

PHP Code:
 'amt' => if($certid==1,8500 else 300 

Wouter60 writes it more concisely by using a ternary statement instead of an "if/else" statement
PHP Code:
 'amt' => $certid==8500 300 
Reply
#4

Quote:The key point that @Wouter60 makes (but does not explain) is that you assign a value of 1 to $certid

Actually that wasn't my key point.
The syntax
PHP Code:
'amt' => if($certid=1,8500 else 300 
is wrong in every aspect (only one bracket, a comma at the wrong place, a single equal sign, etc.). It would certainly fail to get one value or another, based on a condition.

The syntax I suggested is:
Quote:<condition> ? <value1> : <value2>

This is a very userful one-line operation, in human language: if <condition> is true, then return value1, else return value 2.
Reply
#5

(04-26-2019, 07:09 AM)Wouter60 Wrote: The syntax
PHP Code:
'amt' => if($certid=1,8500 else 300 
is wrong in every aspect (only one bracket, a comma at the wrong place, a single equal sign, etc.). It would certainly fail to get one value or another, based on a condition.

Yeah. Well. There is that. Blush
Forgive me for trying to add an explanation to your terse (and entirely correct) reply. Angel
Reply




Theme © iAndrew 2016 - Forum software by © MyBB