Jump to content

[SOLVED] ternary statements


runnerjp

Recommended Posts

i know that they work like this

 

but could it be also used in a code such as..

 

 <?php if ($getthreads3['forumlock'] == 
1) 
        { 
            echo ' 1'; 
        } 
else
{ 
            echo ' 2'; 
        } 
      ?>

 

im just wanting to learn so it cuts my code down and makes me a little more efficient :)

Link to comment
https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/
Share on other sites

Use less whitespace. That's all you can do. For example I would write it like this:

if ($getthreads3['forumlock'] == 1) { 
echo ' 1'; 
} else { 
echo ' 2'; 
} 

 

You can also use an IDE with code templates. For example I use NetBeans, where I can type 'if' and press tab, and I get

if (condition) {
;
}

with cursor placed over 'condition' so that I can type it right away, and then when I press enter I go straight before semicolon;

Speeds things a little :)

 

Nice thing is one can edit those templates to his/hers own liking.

Ok. This will work for echo. (I knew that, srsly :P)

 

I was talking in more general terms i.e. you can't do this with blocks of code.

 

what do you mean?

 

I mean you will not be able to use ternary operator for something like this:

 

if ($getthreads3['forumlock'] == 1)  { 
echo ' 1';
doSomething(1);
} else { 
echo ' 2'; 
doSomethingElse(2);
} 

 

The only place you can use it, is where a statement is evaluated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.