0

I am new to PHP. I have a simple API written in PHP which receives the data in JSON. I am converting the JSON into a PHP array and parsing it:

$obj = json_decode($json,true);

$demircap = $obj['demircap'];
$disuzun1 = $obj['disuzun1'];
$boy = $obj['boy'];
$demiradet = $obj['demiradet'];

I would like to define a new empty object ($disadet) here and set a conditional state of the $demiradet object. If it has a special String value, I would like to give my new object $disadet as the double value of $demiradet, such that if $disuzun1 == 'a' , $disadet == $demiradet*2.

This is my code:

if ($disuzun1 == 'a' || $disuzun1 == 'b' || $disuzun1 == 'c'){
    $disadet == 2*$demiradet;
}else:
    $disadet == $demiradet;

How can I do this conditional formatting with PHP?

Thanks.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
canarcho
  • 81
  • 1
  • 2
  • 12
  • 3
    Does this answer your question? [The 3 different equals](https://stackoverflow.com/questions/2063480/the-3-different-equals). `$disadet == 2*$demiradet;` should be using `=` not `==`. Same for `$disadet == $demiradet;` – ADyson Apr 10 '21 at 07:06

2 Answers2

2

You have some logical errors, the problem is that your code "compiles" as in "is syntactically correct" but it's certainly not what you intended to do.

  • == is a comparison operator, you want to use the assignment operator =
  • : is used in python code blocks, in php you want to use braces { ... } to define code blocks (such as an else code block)

So your code would be:

if ($disuzun1 == 'a' || $disuzun1 == 'b' || $disuzun1 == 'c'){ 
    $disadet = 2 * $demiradet;
} else {
    $disadet = $demiradet;
}

I would actually write this as:

$disadet = $demiradet; // assign the default value (no else required)
if ($disuzun1 == 'a' || $disuzun1 == 'b' || $disuzun1 == 'c'){ 
    $disadet = 2 * $demiradet;
}

Or, including Ivan86's answer to do easily maintainable "is-in-array" lookups:

$disadet = $demiradet; // assign the default value (no else required)
if (in_array($disuzun1, ['a', 'b', 'c'])){ 
    $disadet = 2 * $demiradet;
}
Oerd
  • 2,256
  • 1
  • 21
  • 35
  • 1
    i think my fault was to use == operant, thanks, this works fine. – canarcho Apr 10 '21 at 07:36
  • another question ; here the $demiradet seems a PHP Array object. Can i use arithmetic operator directly with this? or should i do a type conversion? – canarcho Apr 10 '21 at 10:31
  • 1
    Even if you can, you wouldn't want to :-) What is the value of `$demiradet` if you try to `print_r($demiradet)` ? – Oerd Apr 10 '21 at 10:58
  • i cannot print yet, because i call this api from a mobile app frontend. isnt it an array object? i need to send this to an sql query. but i still couldnt, and i thought this was because of data type. – canarcho Apr 10 '21 at 11:09
  • 1
    You can print and look at the backend logs. If you are calling that API yourself then `$demiradet` will have the same type that you set it to in the json (i.e. array, string, int, etc.) – Oerd Apr 10 '21 at 11:10
1

You can simplify your code like this:

if (in_array($disuzun1, ['a', 'b', 'c'])) {
    $disadet = 2*$demiradet;
} else {
    $disadet = $demiradet;
}

Also, when assigning a value to a variable you should use the assignment operator = and not the comparison operator ==.

Ivan86
  • 5,695
  • 2
  • 14
  • 30