• Hi,

    i have a question around variable vs hard code numbers in a function

    this function is working just fine:

    
    if (($budget > "4000" ) && ($freqncy <= "2" || $rdrftlvl <= "2" || $rdrtchskl <= "2")) {
    		$adjbdgt = $budget * "0.75";
    	  }
    	  else {
    		$adjbdgt = $budget;
    	  }
    

    however when I change the hard code numbers to variables as follow:

    
    // adjust budget if budget is above min-budget-limit and the user select low frequency or low fitness or low thecnical
    	  if (($budget > $mnbdgtlmt) && ($freqncy <= $minval || $rdrftlvl <= $minval || $rdrtchskl <= $minval)) {
    		$adjbdgt = $budget * $prcntg;
    	  }
    	  else {
    		$adjbdgt = $budget;
    	  }
    

    its not working. what am I missing here?
    Thanks,

    David

    • This topic was modified 6 years, 1 month ago by davidzohar.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • What are the values of the variables, and how are you setting them?

    Also, in your first block you could have issues with incorrect results because you’re using Strings rather than Integers or Floats for the numbers. Don’t put quotes around numbers when comparing or calculating with them.

    Thread Starter davidzohar

    (@davidzohar)

    HI Jacob,

    I call the values of all the variables using the get_post_meta method in the following manner.
    $mnbdgtlmt = get_post_meta($post_id, 'wpcf-min-budget-limit', true);

    the values are custom field (set as number) in a custom post and expected to be
    $mnbdgtlmt = 4000
    $minval = 2
    $prcntg = 0.75

    BTW thanks for calling out the potential issue with Strings vs. Integers.

    Thanks,

    David

    • This reply was modified 6 years, 1 month ago by davidzohar.
    Moderator bcworkz

    (@bcworkz)

    If you’re getting post meta, then what or how the data is saved is apparently not what you expect. I suggest that you var_dump each variable after getting it from post meta so you can see what you are actually getting. Then you can decide how to construct your conditional tests so the comparisons work correctly.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘virables in function vs real numbers’ is closed to new replies.