It's smart to have your program say
"there IS in stock" or
"there ARE 2 in stock" ... but if you write your code using an "if" and "else" type structure it can become quite verbose.
The
? ... : operator - available in many languages including C, Perl and PHP - allows you to write a single "if,else" line in a much shorter form:
{condition} ? {value if true} : {value if false}
Here's an subtle example of this operator in use - an image from our pricing for a two day public course:
where you'll note "room" and "rooms". The code, where $ns is a variable containing the number of students:
$rlet = ($ns==1) ? "":"s";
$pricing[$ns] .= "<b>With hotel room$rlet<b><br>";
(written 2007-05-18)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P206 - Perl - More Loops and Conditionals [138] Perl - redo and last without a loop - (2004-12-02)
[299] What - no switch or case statement? - (2005-05-03)
[657] The ternary operator in Python - (2006-03-25)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[2815] switch and case, or given and when in Perl - (2010-06-17)
[2817] Setting a safety net or fallback value in Perl - (2010-06-19)
[2824] A pint of Black Rat, and a lazy barman - (2010-06-25)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
[2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3619] Ruby v Perl - a comparison example - (2012-02-21)
[3914] While, for, foreach or something else to loop. - (2012-11-06)
P204 - Perl - Conditionals and Loops [353] Wimbledon Neck - (2005-06-20)
[930] -> , >= and => in Perl - (2006-11-18)
[1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1607] Learning to program in Perl - (2008-04-11)
[1727] Equality and looks like tests - Perl - (2008-07-29)
[2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[4031] Showing what programming errors look like - web site pitfall - (2013-03-06)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
H104 - PHP - Control Statements [340] Code and code maintainance efficiency - (2005-06-08)
[406] Assignment, equality and identity in PHP - (2005-08-08)
[421] Don't repeat code - use loops or functions - (2005-08-21)
[863] Double and Triple equals operator in PHP - (2006-09-12)
[1199] Testing for one of a list of values. - (2007-05-22)
[2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
[2912] Predictions for the seagull population - (2010-08-09)
C203 - C and C based languages - Conditionals and Loops [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[2570] Function Prototypes in C - (2010-01-11)
[3121] New year, new C Course - (2011-01-05)
[3243] Breaking the running sequence - an introduction to conditional statements and loops - (2011-04-11)
[3384] Loops - a comparison of goto, while and for - (2011-08-10)
[4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
Some other Articles
Regular Express PrimerDrawing hands on a clock face - PHPDangerous Dogs and Hotel MarketingWhat are WEB-INF and META-INF directories?Smart English Output - via PHP and Perl ? : operatorSave the Forum - A regular clean sweepMeet, greet and welcomeWhat shape is your shake?Updating a page strictly every minute (PHP, Perl)Two new pages / sites