Jump to content

New to PHP - question on syntax


westside
Go to solution Solved by Jacques1,

Recommended Posts

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 by westside
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by westside
Link to comment
Share on other sites

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 by westside
Link to comment
Share on other sites

  • Solution

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 by Jacques1
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.