Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
PHP module H104
Exercises, examples and other material relating to training module H104. This topic is presented on public courses Learning to program in PHP
, PHP Programming
, Beginning PHP - weekend course / hobby / club / leisure users
As your program grows, you'll want to repeat sections of your code, or only run some sections in certain circumstances. Loop statements, such as while, and conditional statements, such as if, give you these facilities.
|Articles and tips on this subject||updated|
|3914||While, for, foreach or something else to loop.|
Newcomers to programming often ask "which loop should I use?"
Every language has a while loop, most have a for loop (though sometimes those differ in what they do - Python is an exceptional one, for example), and many have a foreach loop. Then you get the odd ones such as the until loop in Perl ...
|3895||Flowchart to program - learning to program with Well House|
Drawing a flowchart is how many people understand a process; it's something that's been used in schools and textbooks for many generations, and it provides a graphic representation of choices and actions that most people find easy to follow.
For newcomers to programming, such as on my Learning to program ...
|3397||Does a for loop evaluate its end condition once, or on every iteration?|
All the languages that we teach have a for loop or the equivalent, which is a clean way of repeating a block of code with a rising or falling index number. It's used in many circumstances - for example in iterating through the months of the year (for m goes from 1 to 12) of in stepping through all ...
|3200||How a for loop works Java, Perl and other languages|
Java, Perl, PHP, Ruby, C, C++, Tcl and many other languages support a "for" loop construct.
When your program enters the top of the loop, it performs the first statement in the brackets ... in this Perl example, that's to initialise the $now variable to 1. It then evaluates the expression given as ...
|962||Breaking a loop - Ruby and other languages|
When you're in a loop there are occasions you want to say get me our of this loop NOW, or "I'm done with the current iteration. And those are the 'classic' break and continue statements from C, C++ and Java. Languages like Perl changed break to last and continue to next ... and added a redo that asks ...
|2912||Predictions for the seagull population|
It was the sound of Seagulls outside (what sound do seagulls make? [link]) Well House Manor that go me wondering yesterday "why have we got so many seagulls around here, when we're so far inland". And as I was teaching a "learning to program in PHP" day, I did a bit of research to find out something ...
|1199||Testing for one of a list of values.|
Question [PHP] When you have a string of conditions in an 'if' clause and one side of the conditional expression is the same in each condition, is there any shortcut to avoid writing out every expression in full. For examples, rather than writing
|657||The ternary operator in Python|
The ? : operator that you may have come across in Perl, PHP, C and Java - known as the ternary or conditional operator - is ABSENT from Python. "But it's so useful" I hear you cry. Ah yes, but isn't this elegant:
val = float(raw_input("Age: "))
status = ("working","retired")[val>65]
print "You should ...
|2304||Extracting real data from an exported file in PHP or Perl|
"Just give me the data". Where there's a requirement to take data from another program into a Perl or PHP script, it's often easier simply to take the data in a format that it's offered / exported from that other program and use code in Perl or PHP to make appropriate use of that data. The alternative ...
|1825||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 ...
|1696||Saying NOT in Perl, PHP, Python, Lua ...|
"Isn't there one standard way to say NOT?" asked one of my delegates on today's course - and it's an excellent question. But the answer to a question about a negative subject is itself in the negative - no, there isn't just a single way!
In fact .. I can think of no fewer that 12 ways!
• 1. ! ...
|1477||Decisions - small ones, or big ones?|
When you're traveling, you'll sometimes come to a point at which you make a decision - to go one way or to go another. Sometimes the decision is a small one - for example, if I'm driving through Marlborough there's a choice of the town centre or the road around the back; whichever I take, they come ...
|1220||for loop - how it works (Perl, PHP, Java, C, etc)|
When writing a program, you'll often want to repeat a block of code, counting up through a table or performing a block of code with an input value (loop counter) that goes up 1, 2, 3, 4 etc.
You COULD do this using a while loop, but this means you have to specify each of
• how to start (initialise) ...
|1191||Smart English Output - via PHP and Perl ? : operator|
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" ...
|863||Double and Triple equals operator in PHP|
Have you been given a piece of code to maintain that has single, double and treble equal operators in it? What's the difference?
A single = sign is an assignment - take what's on the right as an expression and save it in the variable named on the left.
A double = sign is a comparison and tests whether ...
|421||Don't repeat code - use loops or functions|
I've just answered a question about completing a whole series of radio boxes in a form, where the questioner provided sample code that had the same line, slightly modified, appearing time and time again. But if you find yourself repeating something, there must be an easier way and that way will be either ...
Examples from our training material
PicturesThe ++ operator in PHP - pre-increment and post-increment
While and for, last, next and redo
while and do ... while loops - PHP
increment and decrement operators - PHP
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from http://www.training-notes.co.uk
Topics covered in this module
Conditional statements - if.
blocks, elseif and else.
Conditional statement - switch.
Loops - the while statement.
Making up a table in your PHP.
Alternative loop - the for statement.
Other control statement subjects.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.