Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Question Mark - Colon operator (Perl and PHP)

The ? and : operator in Perl and PHP allows you to write a single statement that's both an if and an else without the need for all the clutter of keywords, extra variables, and so on if all you want to do is come up with two alternative words.

Here's an example in PHP - throwing a number on a die, and telling the user to proceed if the value thrown is a five or six, but to try again is it's a 1, 2, 3 or 4:

<?php
 
# Throw a die
 
$throw = rand(1,6);
 
print ("You threw a $throw which means you should ".
  (($throw > 4) ? "proceed" : "try again").
  " at the next turn\n");
 
?>


Here's how it works ("all our code is tested" as I wrote in an email to someone yesterday!)

Dorothy:p1 grahamellis$ php pz
You threw a 3 which means you should try again at the next turn
Dorothy:p1 grahamellis$ php pz
You threw a 6 which means you should proceed at the next turn
Dorothy:p1 grahamellis$


Here is the same functionallity using if and else:

<?php
 
# Throw a die
 
$throw = rand(1,6);
 
if ($throw > 4) {
  $action = "proceed";
} else {
  $action = "try again";
}
 
print ("You threw a $throw which means you should ".
  "$action at the next turn\n");
 
?>


In Perl (I'm running a Learning to Program in Perl course this week, which is where this article originated!), you have the same thing - and a wide variety of other convenient alternatives to if for conditionals and while for loops. I've posted up an example that uses until and also the ? : pairing at http://www.wellho.net/resources/ex.php4?item=p206/golf ... in this demonstration, you enter the number of individuals or teams you have signed up for a knock out competition, and the program tells you what your target should be if you want a perfect knockout with no need for any "byes" in the first round. Here it is running:

Dorothy:p1 grahamellis$ perl golf
How many people? 6
You really need 8
Dorothy:p1 grahamellis$ perl golf
How many people? 8
You're fine as you are with 8
Dorothy:p1 grahamellis$ perl golf
How many people? 11
You really need 16
Dorothy:p1 grahamellis$


You'll note - once again - the subtle use of ? : in the code to change the text "you'll really need ..." into "you're fine as you are with ..." - oh the power of Perl and its plethora of operators.
(written 2008-10-08)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H104 - PHP - Control Statements
  [3914] While, for, foreach or something else to loop. - (2012-11-06)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [2912] Predictions for the seagull population - (2010-08-09)
  [2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1199] Testing for one of a list of values. - (2007-05-22)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [863] Double and Triple equals operator in PHP - (2006-09-12)
  [657] The ternary operator in Python - (2006-03-25)
  [421] Don't repeat code - use loops or functions - (2005-08-21)
  [406] Assignment, equality and identity in PHP - (2005-08-08)
  [353] Wimbledon Neck - (2005-06-20)
  [340] Code and code maintainance efficiency - (2005-06-08)

P206 - Perl - More Loops and Conditionals
  [3619] Ruby v Perl - a comparison example - (2012-02-21)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
  [2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2824] A pint of Black Rat, and a lazy barman - (2010-06-25)
  [2817] Setting a safety net or fallback value in Perl - (2010-06-19)
  [2815] switch and case, or given and when in Perl - (2010-06-17)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [299] What - no switch or case statement? - (2005-05-03)
  [138] Perl - redo and last without a loop - (2004-12-02)


Back to
Which is your best hotel room?
Previous and next
or
Horse's mouth home
Forward to
Perl - Subs, Chop v Chomp, => v ,
Some other Articles
Dont bother to write a Perl program
Perl - map to process every member of a list (array)
What a shock
Perl - Subs, Chop v Chomp, => v ,
Question Mark - Colon operator (Perl and PHP)
Which is your best hotel room?
FSB - an update.
Claverton Pump
Autumn
Sorting objects in PHP
4300 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1825_Que ... -PHP-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat