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

Link to comment
Share on other sites

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.

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.