Computers are really good at repetitive tasks. When a task is repeated, a loop is made and the programme set to repeat the tasks within the loop until it fulfills some limits set. These can be a number of loop cycles, using a counter. Or it could be a condition: e.g. 'keep adding numbers until the total is greater than or equal to 100'. At the conclusion of the count or when the condition is met, the programme exits the loop and continues with the script that follows.
There are different ways to make loops, depending on the application in question:
for loops often use a counter, so that the loop executes a precise number of times.
This code creates a dropdown select menu with the options the years 2000-2020, inclusive.
As opposed to
while loops are not limited to a set number of iterations, instead will remain (potentially infinitely) looping till a set condition is no longer true. Care must therefore be taken not to create an infinite loop - one that cannot be fulfilled, so the programme enters a loop and never exits, in a sort of black hole logic trap.
while loop will not execute if the condition is never true.
while loops are used to retrieve data from a database, particularly when it is not known how many records there are.
This code comes after a Mysql query has returned a set of records from a database. It takes each record in turn from the returned array, assigns it the variable $row, and executes some code.
For example, depending on the gender of the visitor, a printout chooses between the 'Dear Sir' salutation, rather than 'Dear Madam'. The programme simply states: "If visitor is male, print 'Dear Sir', if not 'Dear Madam'."
In PHP the syntax is:
A logic chain could have more than two possible conditions, and can check them in sequence with an 'elseif' clause:
|&&||and||\$var1 && $var2|
|||||or||\$var1 || $var2|
|XOR||and not||\$var1 XOR $var2|
|==||is equal to||$var1 == 42|
|!=||is not equal to||$var1 != 42|
|<||less than||$var1 < 42|
|>||greater than||$var1 > 42|
|<=||less than or equal to||\$var1 <= $var2|
|>=||greater than or equal to||\$var1 >= $var2|
\$variable1 is true if \$variable1 has value that is not: 0, an empty string, FALSE or NULL.
isset(\$variable1) is a function that checks the \$variable1 has a value that is not NULL. NB. if the value in \$variable1 is 0, an empty string, FALSE, the isset function will return true. NULL means no set value.
isset() is useful for checking if an input has been made to a field in a form, which may or may not be necessary. If the field has been left by the user, then isset() will return false, and a different action can be set. For example, no input may invalidate the form (for mandatory fields, such as name), or a default value can be set (such as an existing $user_name, imported for this user from some other page on the site).
if-elseif-else conditional clauses are in theory unlimited in length. However, they can get confusing and cause errors. As a substitute, PHP provides a conditional which 'switches' through a series of possible values for a variable, and executes the following code if there is a match. If no match is found in the list, an optional default code is executed.
isset() returns true for the empty field ''. This means it cannot be used to check that a form input field has been completed correctly.
empty(), on the other hand, checks to make sure there is a string in the variable returned from a field submitted by a form, and not '', 0, NULL, or FALSE.
Content © Renewable-Media.com. All rights reserved. Created : August 12, 2014 Last updated :February 14, 2016
The most recent article is:
View this item in the topic:
and many more articles in the subject:
Science resources on ScienceLibrary.info. Games, puzzles, enigmas, internet resources, science fiction and fact, the weird and the wonderful things about the natural world. Have fun while learning Science with ScienceLibrary.info.
1916 - 2004
Maurice Wilkins, 1916 - 2004, molecular biologist, was 'the third man of the double helix', as his biography title declares. Born in New Zealand, but did most of his professional work in England, Wilkins shared the Nobel Prize with Crick and Watson.
In theory there is no difference between theory and practice, but in practice there is.
Website © renewable-media.com | Designed by: Andrew Bone