Vous êtes sur la page 1sur 1

Chapter 4 More complex PHP Data handlers Arrays,Hashes and Functions

(Chapter 3) More complex PHP program operation code

Statement flow control (comparisons and decision making in PHP code)

There are a few contexts to php program control such as the php.ini file and PHP's ability to modify its configuration
settings using it's pre-defined functions but that type of thing is not it. This is about a systematic operation upon data
by using decisions in program code. The first to show you is defined as a data type not simply an operation. It is one of
the most basic and important in programming in all languages. It is called a boolean operation.
When a boolean decision is made the operation returns one of two results and are the settings for the data type as
keyword value of either true or false.
PHP test operations and test functions produce a variety of test return result settings of around 5 different data type or
setting.
An integer, incremental and calibrational results such as integer
NULL representing no defined result of a test,
boolean, logical true or false,
Regular Expression, complex text pattern matching, and character set retrieval
Return results (usually boolean) from high definition specialised test functions to determine an actual data typing
situation. e.g. ctype_upper() function returns true if all text fed to it is upper case.

<?php
$num_test=0;
$para="<p>";
if($num_test==1){
print('$num_test equals 1 and is true');
}else{
print('$num_test equals 0 and is false');
}
?>

notice the two equals signs together in the if test statement, to obtain the opposite channelling result by coding
you would change them to != (is not equal to). The exclamation mark means (is not).Note here that the comparison between the
integer literal 1 and the variable is written as != and not written as !== as you will later see. The second method !==
is for a boolean test from a function test result and not against a pair of standard variables containing some definite data type.
The reason for == above in the if test is to explicitise the comparison, to write simply one equal = sign is only symbolic of
assigning a value and would cause a runtime error.

Often this system is used to operate a more complex program channelling flow control than simply to channel A or to channel B
in an operation called a loop as next below.

<?php
for($c=0;$c < 10;$c++){
print('loop run number is '.$c.' : and will not print the number 10'."<br>");
}
?>

Effectively it means however operation continues(true) or ceases if false is returned at some time. The $c < 10 has
the less than operator < (called an angle bracket) in the middle of it causing the loop to stop when $c++ with the two plus signs
together ++(called the post increment numeric operator) reaches 10 in the for loop making the condition for the loop
running false. At the end of each iteration of the loop the ++ operator causes $c to have the value of 1 added to $c and then
the loop condition is checked for a boolean against $c by the test statement arrangement.

A system similar to a true and false system is a switch. A switch is more of a function because you pass it an argument
for it to check if it matches (e.g. alike to == equals sign pair in an if ) a set case to be channelled through and if
it cannot find a match it can have a default channel to complete its way through.

<?php
$c="four";
switch($c){
case "minusone": print('this is not worth four '.$c .'<br>'); break;
case "zero": print('this is not worth four '.$c .'<br>'); break;
case "one": print('this is not worth four '.$c .'<br>'); break;
case "two": print('this is not worth four '.$c .'<br>'); break;
case "three": print('this is not worth four '.$c .'<br>'); break;
default
print('this is worth four '.$c.'&'.'nbsp;');
}
?>

Various types of true and false systems exist as stated before. Another is called a truth table. In a truth table a special
operator called a broken pipe operator | (or pipe operator as short) is used to make a choice as a pair || to find if any
set of variables are accepted in the boolean test. i.e. If one of them is not but the next is then the test remains true.

Here is how it appears, not much different to a standard if operation, But below it appears a little over complicated.
That is only because two things are required to be shown here. First, An if statement as before is only concerned with obtaining
from its tests either TRUE or FALSE not whether something is or is not something you perceive it to be in the real world or the
conceptual world of data. It does not know anything other than a legitimate question has been asked to return either 0(zero)
meaning FALSE or 1(one) meaning TRUE(there is minor catch in this function because of FALSE also returning an integer value
of zero from stripos() function of makes it difficult to discern true from false in text string handling See the PHP Manual about
strpos() and stripos() for more information).
Now notice the pre-defined function stripos() in the if wrapped again by the parenthesis (instruction operation separator)
operator pair. stripos() is a special text string function to find whether a non-case-sensitive string can be found inside
a comparison text string that a both defined for this purpose inside the stripos() function.It returns FALSE if not found.
Any of the $choice* variables are the string to be tested,and, $itsa has the string to look for in those(NOTE: $choicE).
Second, when the operations start the first in the sequence is test and then the next if the previous did not succeed the
if returning true. If it reaches any that are true it will then terminate testing and choose the first channel. If it
completely fails to find a TRUE it will then execute the else section of the statement. This truth table system is called
a LOGICAL EITHER-OR. With this special system and function the !==false operator test means find if it will return TRUE from
the test.

<?php
############### NOTE: stripos() FUNCTION SYSTEM HERE IS PHP5 and can return integer or boolean
$itsa='VEGETABLE';
$choicA='APPLE';
$choicB='PAIR';
$choicC='BANANA';
$choicD='POTATO';
$choicE=$itsa;
$choicF='TOMATO';
if((stripos($choicA,$itsa)!==false)||(stripos($choicB,$itsa)!==false)||(stripos($choicC,$itsa)!==false)||(stripos($choicD,$itsa)!==false)){
print('You should not find this text from the outer IF');
}else{
## putting an if else inside another is called compounding(this is a compound if-else)
if((stripos($choicE,$itsa)!==false)||(stripos($choicF,$itsa)!==false)){
print('This inner IF is where the test should return TRUE ($choicE=$itsa) $choicE was assigned the value VEGETABLE');
}else{
print('This inner ELSE should not be operated');
}
print('This outer ELSE should not be operated');
}
?>

Finally one more useful operator exists in truth table systems and is the & ampersand referred to as AND.An
&& pair are useful in boolean operations to ensure that both or more tested values are TRUE, to return
an if or other boolean test TRUE(or false as the operators used require).

<?php
############### NOTE: stripos() FUNCTION SYSTEM HERE IS PHP5 and can return integer or boolean
$itsa='VEGETABLE';
$choicA=$itsa;
$choicB="VegETaBle";
$choicC=$itsa;
if((stripos($choicA,$itsa)!==false)&&(stripos($choicB,$itsa)!==false)&&(stripos($choicC,$itsa)!==false)){
print('They are all set to the exact same as $itsa and the boolean test has returned TRUE');
}else{
print('This should not execute');
}
?>

Now that we can decide some action with a small ability to commit comparrison of data to a template of its desired parameters,
it can be put to some basic use. Now to build an HTML form with one text field and one button to send some text.
In the previuos chapter i explained text can be sent by a form and specifically removed from
whichever form field wanted,but, before you didn't know how to perform a simple decision to channel the PHP script flow to suite
the situation.
e.g. If the surfer that finds the HTML form were to not fill in the form but simply press the button they potentially are either
not aware of their mistake or do not realise that the form has been sent. In this small script it will not be disasterous but in
the more real world problem of information collection and forms data loss dependent the setup system created in the script to
process the form would be serious, so this form will return a warning message to the user by logical channelling if there is no input
to the text field and will return the somewhat simplistic action design along the actions of the previous address bar script.

The complete script will start with an if-else and inside it will have a new function to it called isset() and also strlen().

isset() is a test function to know if the variable has been set.


strlen() is a function to return the length of the text in numbers of characters.

<?php
########### channel-form.php ################
$page_title="my pages title"; # the words my pages title can be changed to any text you want
$html_head_begin = "<html>"."\n"."<br>"."<head>"."\n"."<br>"."<meta name=\"generator\" content=\"nicephot.xlphp.net php tutorial\">"."<title>";
$notcor='<h2>YOU DID NOT CORRECTLY OPERATE THE FORM</h2>';
$formwrt='Write in your full name here: <form name=fnm method=get action=channel-form.php>'."\n"."<input type=\"text\" name=\"sometext\" size=\"30\"
value=\"\">"."\n"."<input type=\"hidden\" name=\"validsend\" value=\"validsend\">".'<input type=submit value=send><p>';
## global variables finnished
print($html_head_begin.$page_title);
print('</title></head><body><center><p>'."\n"."<br>");
## html page start
print('<p>'); ## some html know how
if((!isset($HTTP_GET_VARS['sometext'])) || (!isset($HTTP_GET_VARS['validsend']))){
## if the script has never been called just render the form
print('<h2>WELCOME</h2>'.$formwrt);
}elseif(strlen($HTTP_GET_VARS['sometext']) < 5){
### if the form is coming in but is logically(human language grammer) short of content
### this will catch it before it reaches the printout name function
print($notcor.$formwrt);
}elseif(isset($HTTP_GET_VARS['sometext']) && isset($HTTP_GET_VARS['validsend'])){
#### seems ok!
print('<h2>WELCOME</h2>'.$formwrt);
print('Your full name is: '.$HTTP_GET_VARS['sometext'].'<p>');
}else{}
### output the end html
print('</p></center></body></html>');
?>

Vous aimerez peut-être aussi