• Resolved dune1982

    (@dune1982)


    Hello guys,
    I tried to make a function that changes what is written in a variable.
    See code:

    function wc_change_billing_country( $order ) {
    	if ($order->billing_country = 'DE'){
    		$order->billing_country = "Germany";
    		return $order->billing_country;
    	}
        if ($order->billing_country = 'CH'){
            $order->billing_country = "Hurliburli";
    		return $order->billing_country;
    	}
    	if ($order->billing_country = 'A'){
            $order->billing_country = "Austria";
    		return $order->billing_country;
    	}
    
    }

    but this function seems to always return the first if case (DE)
    what did I do wrong in the code?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Dion

    (@diondesigns)

    Here’s a fixed version of your code. “Equal” comparisons require two or three equal signs depending on the comparison. In your case you only need two. In addition, your code should use “else if” on the second and third comparisons since there is no need to do the comparison if one above it was successful.

    function wc_change_billing_country($order) {
    	if ($order->billing_country == 'DE') {
    		$order->billing_country = "Germany";
    	}
    	else if ($order->billing_country == 'CH') {
    		$order->billing_country = "Hurliburli";
    	}
    	else if ($order->billing_country == 'A') {
    		$order->billing_country = "Austria";
    	}
    
    	return $order->billing_country;
    }

    I also changed the layout of the function. If the billing country was something other than the three in your comparisons, your original function would return nothing, and that might break the code which called the function.

    You could also use a switch statement instead of the multiple elseif checks…

    function wc_change_billing_country($order) {
        switch($order) {
            case "DE":
                $order->billing_country = "Germany";
                break;
            case "CH":
                $order->billing_country = "Hurliburli";
                break;
            case "A":
                $order->billing_country = "Austria";
                break;
        }
        return $order->billing_country;
    }
    Thread Starter dune1982

    (@dune1982)

    Thank you so much guys for your input.
    @diondesigns your code works perfect thanks for the hint.

    @wspencer your code did not work, it seems like it is changing nothing as the result is DE or CH or A

    Sorry, I typed too quickly. You’re passing in the $order object to the wc_change_billing_country() function. The switch statement should run on the billing_country property of that object…not the object itself. I’ve changed the code below, if you’re interested.

    function wc_change_billing_country($order) {
        switch($order->billing_country) {
            case "DE":
                $order->billing_country = "Germany";
                break;
            case "CH":
                $order->billing_country = "Hurliburli";
                break;
            case "A":
                $order->billing_country = "Austria";
                break;
        }
        return $order->billing_country;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘function and if check in php’ is closed to new replies.