1

I am building a PHP script. From one form I get min_salary and max_salary. I am trying to get both of them as a string in a salary variable like

$salary = "min_salary TO max_salary";

If the min salary is not specified it should be set to 0, similarly if max salary is not specified it should be set to *.

I have written the below code but am not getting anything when either of the fields are not provided.

$salary = (isset($data['min_salary']) ? $data['min_salary'] : 0) .' TO '. (isset($data['max_salary']) ? $data['max_salary']:'*');

Also I don't want to use if then else statements.

5
  • The code looks fine for me, what do you mean with "I have written the below code but not getting anything when any of the field is not provided." - what outputs var_dump($salary) Commented Feb 5, 2014 at 23:19
  • 1
    suppose that the min_salary input box is left blank and max_salary is given 10 then the output should be like "0 TO 10" but I am getting only "TO 10" :( Commented Feb 5, 2014 at 23:21
  • There is a difference between the methods "isset" an "empty" - be aware of it. If you want to check whether is empty replace "isset(.." with "!empty(..." Commented Feb 5, 2014 at 23:23
  • You're already using if-else statements. Did you try casting the min_salary variable to integer? Commented Feb 5, 2014 at 23:25
  • 1
    Thanks Flixer your answer works fine just replaced isset with !empty Commented Feb 5, 2014 at 23:35

4 Answers 4

2

This line would produce that code without using if/else statements (even though ternery operator are really syntactic sugar around if/else statements)

$salary = max(0,$data['min_salary']) . ' TO ' . ($data['max_salary'] > 0 ? $data['max_salary'] : '*');

You don't really want the same scripting for both values as one should fallback to 0 and the other to *. The problem with isset():

(isset($data['min_salary']) ? $data['min_salary'] : 0)

is that a variable can be set to an empty string $data = '' which would return true. I'd hazard you do not want this to happen.

Sign up to request clarification or add additional context in comments.

1 Comment

Right, this may be an other alternative to right the same. But I got my answer thanks for all your help.
2

Thanks Flixer and Felix for your answer it works fine after I replaced isset with !empty

$salary = (!empty($data['min_salary']) ? $data['min_salary'] : 0).' TO '. (!empty($data['max_salary']) ? $data['max_salary']:'*');

Comments

1

Cast the values to int:

$salary = (isset($data['min_salary']) ? (int)$data['min_salary'] : 0).' TO '...

Same for the second part...

1 Comment

Thanks but not work. Anyways I got answer from Flixer.
-1

Your statement is valid and provides a string, it works as-is on my php server (php 5.3.7) with $data not defined or defined and containing only one value.

Maybe it's the way you use $salary that has a concern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.