PHP if echo else echo using first bracket

I’d like to make an if-else condition in PHP using the first bracket only. Here’s the code I’m using currently:

if ( is_single() ) echo ' singlular';

I want to make an else statement within this code. I want to add else echo ' archive' conditional. How can I do that?

The Free Online PHP Manual is always good for finding information:

Try a browser search for “PHP if else”

https://www.php.net/manual/en/control-structures.else.php

Edit:

Let us know your solution :slight_smile:

This seems working:

if ( is_single() ) { echo ' singular'; } else { echo ' archive';}

Is everything right?

1 Like

I’ve amended my post and instead of just the PHP if statement i’ve added PHP if else with a link to the PHP Manual.

Try this:

if ( is_single() )
{
  echo ' singlular' 
}else{
  echo  ' archive';
}

// or this IF AND ONLY THERE IS JUST A SINGLE STATEMENT
if ( is_single() )
  echo ' singlular' 
else
  echo  ' archive';


// or this method which I prefer because it is far easier than trying to find where curly brackets start and finish
if ( is_single() ) :
  echo ' singlular' 
else :
  echo  ' archive';
endif;

// or even this as a one-line
echo  is_single()   ?  ' singlular'   :  ' archive';

Please consult the PHP Manual because if/else/endif are frequently used and should be mastered.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.