westside Posted September 11, 2014 Share Posted September 11, 2014 (edited) Hi, I'm very new to PHP, 2 days in fact, but I've done some programming in other languages. I'm trying to do something pretty basic here. I've inherited a site built on WordPress and I'm trying to override one of the php files contained in a wordpress plugin to do what I want. I have this line: <label> <?php if ( ! $account_required ) echo '<small>' . __( '(optional)', 'wp-jm' ) . '</small>'; ?> </label> I understand what the code is doing, if condition is false, then output the HTML <small> tag, I want to add and else condition to the above, that outputs the word "required" in place of where the word optional is. So if $account_required = false then shows the optional in parenthesis as it does now, otherwise I want to show the word "required" in the parenthesis. Super simple, but just can't get the syntax quite right. Also, can someone explain the double underscore part, specifically this: __( '(optional)', 'wp-jm' ) I understand in PHP periods are used to concatenate, but what is the leading double underscore right before the word "optional" in parenthesis? it's as if there is some function being called or referenced. The end result when the HTML is rendered is "(optional)", but what's up with the leading underscore and the reference to "wp-jm" I assume that there is some plugin or something... Any help appreciated. Thanks.... Edited September 11, 2014 by westside Quote Link to comment https://forums.phpfreaks.com/topic/291007-new-to-php-question-on-syntax/ Share on other sites More sharing options...
Jacques1 Posted September 11, 2014 Share Posted September 11, 2014 Super simple, but just can't get the syntax quite right. So how does your syntax look like? Also, can someone explain the double underscore part, specifically this: __( '(optional)', 'wp-jm' ) This isn't any special syntax, it's a normal function which happens to be called “__”. And if you have a look at the Workpress manual, you'll see that it's used for localization. Quote Link to comment https://forums.phpfreaks.com/topic/291007-new-to-php-question-on-syntax/#findComment-1490798 Share on other sites More sharing options...
westside Posted September 11, 2014 Author Share Posted September 11, 2014 (edited) Here is the full code block and that may be why what I've tried isn't working. I added curly braces to the if statement and added the else part and closed it with curly braces <?php if ( $registration_enabled ) : ?> <fieldset> <label><?php _e( 'Your email address', 'wp-jm' ); ?> <?php if ( ! $account_required ) { echo '<small>' . __( '(optional)', 'wp-jm' ) . '</small>'; } else { echo '(required)';} ?></label> <div class="field"> <input type="email" class="input-text" name="create_account_email" id="account_email" required placeholder="<?php esc_attr_e( 'you@yourdomain.com', 'wp-jm' ); ?>" value="<?php if ( ! empty( $_POST['create_account_email'] ) ) echo sanitize_text_field( stripslashes( $_POST['create_account_email'] ) ); ?>" /> </div> </fieldset> <?php endif; ?> Edited September 11, 2014 by westside Quote Link to comment https://forums.phpfreaks.com/topic/291007-new-to-php-question-on-syntax/#findComment-1490804 Share on other sites More sharing options...
westside Posted September 11, 2014 Author Share Posted September 11, 2014 (edited) Well, I just had a typo it seems, the above code I just posted seems to work. I had the semi-colon outside of the closing curly brace, I just moved it inside of it, uploaded the php file to the dev server and it appears to work fine.... whadya know If there is a more preferred way to doing what I want, I'd like to know though.. Thanks! Edited September 11, 2014 by westside Quote Link to comment https://forums.phpfreaks.com/topic/291007-new-to-php-question-on-syntax/#findComment-1490806 Share on other sites More sharing options...
Solution Jacques1 Posted September 11, 2014 Solution Share Posted September 11, 2014 (edited) There's no need to use curly braces when the branches only consist of single statements (it does improve readability, though). Another option is to use the ternary operator which yields one of two values depending on a condition: echo $account_required ? '<small>' . __( '(optional)', 'wp-jm' ) . '</small>' : '(required)'; Whether you prefer this or the more verbose if statement is up to you. Edited September 11, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/291007-new-to-php-question-on-syntax/#findComment-1490808 Share on other sites More sharing options...
westside Posted September 11, 2014 Author Share Posted September 11, 2014 Thanks a bunch for the help! Quote Link to comment https://forums.phpfreaks.com/topic/291007-new-to-php-question-on-syntax/#findComment-1490813 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.