Jump to content

Easier and more organized way than if/else if?


slyte33

Recommended Posts

Basically the title says it, but I know there is something with PHP easier than a bunch of lines a code than this:

 

If ($example == 1)
{
}else if ($example == 2)
{
}

 

I know you can make a function to do things easier, but that's selecting something from the DB and using loops.

Just a nice simple if/else if statement, is there an easier way, like 1 line of code or something?

 

It's a noob question, but I am just curious, thanks :)

Link to comment
Share on other sites

That is called the ternary operators (? :) which is just a short if / else statement:

 

$example = ($example2 > 0)?'Greater Than 0!':'Less Than 0!';
echo $example;

 

So basically if example2 is greater than 0 set example to be "Greater Than 0!" else, set it to be "Less than 0!".

 

You cannot do anything more complex then an if/else with the ternary operator.

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.