Main Menu




browser lang:en

Users Area

Client Login

today cache size is:222615

 

Holyguard rss
rss 2.0 for all sections

 

Portfolio

Some of my projects:
CTIBA
MA-NO Web Agency
BluPool
L'Pratone
Travel in Hotel
Yacht Elements
AbruzzoWeb
Il Mastino
CSI Teramo
Innovazione S.p.a.




Comments

John
24/07/2010 by John
better
16/04/2010 by bob
attached files not working !
05/11/2009 by Adnan
Tuvok
14/02/2009 by
Nice script
07/02/2009 by desaj
Other solutions
07/02/2009 by Mike
well
06/02/2009 by holy
Theif
06/02/2009 by

Read More »


RAPIDQ

Rapid-Q:The lost files
My old library where i have collected all the rapidq scripts and italian help


Today my pagerank is: Free Page Rank Tool

Shorthand If/Else using Ternary Operators

An essential part of programming is evaluating conditions using if/else and switch/case statements. If / Else statements are easy to code and global to all languages. If / Else statements are great but they can be too long.

Ternary operator logic is the process of using “(condition) ? (true return value) : (false return value)” statements to shorten your if/else structures.
What Does Ternary Logic Look Like?


/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true

  /* most basic usage */
  $var = 5;
  $var_is_greater_than_two = ($var > 2 ? true : false); // returns true
  

What Are The Advantages of Ternary Logic?

There are some valuable advantages to using this type of logic:

* Makes coding simple if/else logic quicker
* You can do your if/else logic inline with output instead of breaking your output building for if/else statements
* Makes code shorter
* Makes maintaining code quicker, easier
* Job security?

Tips for Using Ternary Operators

Here are a few tips for when using “?:” logic:

* Don’t go more levels deep than what you feel comfortable with maintaining.
* If you work in a team setting, make sure the other programmers understand the code.
* PHP.net recommends avoiding stacking ternary operators. “Is [sic] is recommended that you avoid “stacking” ternary expressions. PHP’s behaviour when using more than one ternary operator within a single statement is non-obvious.”
* If you aren’t experienced with using ternary operators, write your code using if/else first, then translate the code into ?’s and :’s.
* Use enough parenthesis to keep your code organized, but so many that you create “code soup.”

More Sample Usage

Here are a couple more uses of ternary operators, ranging from simple to advanced:

/* another basic usage */
$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');
/* another basic usage */
$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');
/* echo, inline */
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody'); //harsh!

Click here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code

/* echo, inline */
  echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody'); //harsh!
  /* a bit tougher */
  $score = 10;
  $age = 20;
  echo 'Taking into account your age and score, you are: ',($age > 10 ? ($score < 80 ? 'behind' : 'above average') : ($score < 50 ? 'behind' : 'above average')); // returns 'You are behind'
  /* a bit tougher */
  $score = 10;
  $age = 20;
  echo 'Taking into account your age and score, you are: ',($age > 10 ? ($score < 80 ? 'behind' : 'above average') : ($score < 50 ? 'behind' : 'above average')); // returns 'You are behind'
 /* "thankfully-you-don't-need-to-maintain-this" level */
  $days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31)); //returns days in the given month

   /* "thankfully-you-don't-need-to-maintain-this" level */
$days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31)); //returns days in the given month

Rating:
62.0
12 votes
1 2 3 4 5

Comments

Insert your comment

Titolo
Messaggio
Nome Utente
e-mail (se vuoi ricevere le risposte a questo post anche via mail)

Videos


Contents

Selective Tweets Application for Facebook

21/07/2010 

Selective Tweets is an application that allows you to control what tweets are added to your Facebook…

in:Social Networking (0 comments)

FBJS Quick Jump Menu for a FBML Facebook Platform App

15/07/2010 

In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…

in:Scripts and tutorials (0 comments)

How to manage your online reputation, free tools forcommunity managers

02/07/2010 

Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…

in:Scripts and tutorials (0 comments)

Genuitec MobiOne Enables Developers to Create Web Apps for the iPhone

28/05/2010 

Although some IT managers are coming around to the idea of supporting iPhones, most aren’t convinced…

in:Free Software (0 comments)

How to configure MX records for incoming SMTP e-mail traffic

27/03/2010 

When you want to run your own mail server, and it does not matter what version and…

in:Apache - .htaccess (0 comments)

Complete guide for Jquery Developers

27/02/2010 

Have you ever had to develop something yourself only to find out that there had already been…

in:JQuery (0 comments)

How can i insert HTML code in my posts?

14/02/2010 

The situation begins with your blog or website and you need to post some code on a particular…

in:The Holy Faq's (0 comments)

12 undocumented tricks for Google Buzz

14/02/2010 

So. Google just recently announced Google Buzz. I’m not sure about you, but I…

in:Blog (0 comments)

Read more »


Tag Clouds


Selective Tweets Application FacebookFBJS Quick Jump Menu FBML Facebook Platform AppHow manage your online reputation free tools forcommunity managersGenuitec MobiOne Enables Developers Create Apps iPhoneHow configure records incoming SMTP email


Add to Technorati Favorites