clem_dahms Posted November 29, 2022 Share Posted November 29, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315595-small-conditional-php-function/ Share on other sites More sharing options...
gw1500se Posted November 29, 2022 Share Posted November 29, 2022 What is not working and what error are you getting? Do you have error reporting turned on? error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/315595-small-conditional-php-function/#findComment-1603047 Share on other sites More sharing options...
ginerjm Posted November 29, 2022 Share Posted November 29, 2022 (edited) 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 November 29, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315595-small-conditional-php-function/#findComment-1603049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.