Jump to content

Small conditional php function


clem_dahms

Recommended Posts

Hi,

 

I am quite new to php coding and I am trying to write a simple function.

The aim of this function is to print a small bit of html code when it is called.

Also I have 2 languages on my website so I have added an "if" so that I can call the write text depending on the language chosen by the user.

 

Here is the code I have so far but it is not working yet :

 

function delivery_title() {
    $current_lang = get_locale();
    $text_clem = '';
    if ($current_lang == 'fr_FR'): {
        $text_clem = 'LIVRAISON'
    } else: {
        $text_clem = 'DELIVERY'
    }
    endif;

echo '<span id="order_review_heading" class="checkout_header">';
echo $text_clem;
echo '</span>';

}

 

Would you guys be able to help me make this small function work properly ?

Thanks a lot !

 

Very best,

Clément

Link to comment
Share on other sites

When using a function to set a value (which you are doing) one must return the value to the caller.  Here is how I would do it.  Note how I place the function definition below the code that is referencing it.  I keep my functions out of the way of the main line code that does everything.  

$title = delivery_title();
...
...
...
function delivery_title() 
{
	$current_lang = get_locale();
	if ($current_lang == 'fr_FR')
		return 'LIVRAISON';
	else
		return 'DELIVERY';
}

I could have set a variable in the function and then return that as the last line of it, but there was no need in this simple function.

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.