``<, b/l9 >,  lma@abx p stle"bes{zev-alyg6rl(images/top.gIf+baCkbou.d%SEea6:be0met 3s;.p> um}-$i2`kr(hl=ati4/ style='cglOfdE#Df4/C:<qPm> H
misc| $bis Site,/#}ptolv8/Srm>
q xm,ht6p://www.broid juvoz/WN/i.lexl(|M>BroaddNuWwork!$ =axr%n="http//5ww.boa&-netoR}.+nMqmRg>(|mb hG/aa6>eaOe$a0_%mber0 Loghn/a>$ 0& ,c izF9"td0:/+wgu.broad-network.co-/wSite.htm" id="wziap ptn%=#Write"Niyd"lfthar i3l@pul {dries2 3|yle="dI{\g2o-ne":Wbke")mk64.@~  !=xpEf0h|up;/www/boe$=LDgoskcgl/Lout,h}l id="og-utL":stle=di1plCo-.m">LkoWP<> kstrOn.

4/Nev>M +/h'ader> <[bypt typd=te8|/Esi&.  `( ` iG (eCuionPtsag.w0)tgC./Hn==`"~N/@n9 ( $ 0{O(! 0 r `( $daument/oTAhe}wn4jyt!lwdinL').stil%fdiSpay = "none"+MJ( 0(`$ 0do!5met5U0d%EedByAd #o/wU$DW#y&/HeWTxcx i]ine"; 0 p(`( % "do!umet.%etEleMedBy[d`,wjh4mN'i.sUyLadkspLap5 #mnline" f0#b ! } else if -sqsyo.toS!e0eaduR afB9 &lga%#) ! 8 @( 0" { ' S" ( $ p0d-o}|cndlgeUElementById('Do}L'k.S|\e.tkspnaq!= "none;Z ` ( ($4 tocueeNp.getl}OeC;d('lOgetL7n{6yle,darplay = " nYne"? "$"1 } <wcrhp> ; H <(3 Rtyle='text-align:#eoUr'>Re|q4-l Articl%s<h3> a hrug}IntroduotIy.e|o-PHP.htm'>Intrdu!t)nA|O PP+a><p-pclass='la}&,pt3me='':>Q*mf]+XiT-Basmc=Qyntax.htm'>PHP Basic Syntax

e!IwFuRcb1/of PHP-Referejcu,htm'Ba1icy0'(aJP R%feSgnkD5/<`cd`sSy~f|le#> href=%P@Q-OO@-iskc{/hm'|PHPO@Xuag<k>/p,p<.njwX/+q.|p!claSs2n@v">Bra$(et5ork=/? <` #fa{r}bn@W `xred=*!tU}+wgu&jRKed-ndto2cCommLBa.&m">Become a MeabU|<e7nt} >AozeAuU4.a> + H art)clD id='A1'"sxl'='dasPha}:yl,in-oc); widph*5:vW? verticad-h5n:topO>F#Zid t}qeclu;|ECEAsgBil'< b " 8!`(if (ses{iMj{ubae.5rtE4Lofk(<= "logan- $ 0" 1 ( y= $ 'oumentngeUAlu/QX+KD('C'k.stleldisplay = itiOu"{ !` !$%"*! }0L + $!0`  sessionStopaod.wripebKD0=b*:#; " (! $ 8qEfs'SU-rage.articleK(r#309 ! (($ bcc3)o_s0graGuidNn "9  0$0 0 b " '.ajAx % # 0"`0(a( vq0`Pmx4Ut "(0 `h( !$ 0 `(var ctsnvUZM(9 tmcw/eNpUPN;%  B ( $ 0tav"hwi_l_J]fId0ReXuEwt!*!p`h  `( 0 {?/b#odd Ei,"FiSefox !|rO/ 2%[a, Safe2y+ b" (! " ! b "*!K!40`(xmlh|tP$=(nEs XLQtqreest) "$(0b ! h ]( j $ 0 elsedh( $ 0" ( [+/ cedM%fkr0Ke2 F0$$ ! ` hmMhttp = oe`cUitQjDbp6xo[kf.hMPTT@'i3 b $ 0" 0(~M- 0@f" 0 `( $x}nHt`.onready3taUuciwu&](ction() $$ 0" $ 0"  2* 0! b! @  ig 8mhId60.[DAyS6iE$}=(5) " (` & !0(! b! ` (! 0 `(( $b jzMddtp.st`|qS$= 20)O $ 2" $b 0 {  , 1"0 " (# (! ! | 0! `  H  ! lu ( $ 0 0!` { } } } writerID = sessionStorage.writerID; articleID = sessionStorage.articleID; partNo = sessionStorage.partNo; readerID = sessionStorage.readerID; sendStr = 'writerID='+writerID+'&articleID='+articleID+'&partNo='+partNo+'&readerID='+readerID+'&articleURL='+currentURL; xmlhttp.open('post', 'http://www.broad-network.com/pageViews.php', true); xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlhttp.send(sendStr); document.getElementById('edit').href = "http://www.broad-network.com/edit.php?writerID=2&articleID=330&partNo=8";

(adsbyfoglu}gindow.!duCd^le || [U)tush({}); 4/erBql|'Eir:,j3~XHP p0D{p Ba{iC s+dj Sgg}cktyC~erls4.h5>,j2BicK%{!of PIPwth Ce#}rIp onsidus!ko~r@ xdt z
8`8d>Rs+ |i hRe 'd<0WSwr2OijetWo{&cMi'BjQRdhqrVpsb`=1oxrofcLMxtMrX1an4huVqte PublihE": 18 BaN$218_/spen.:. >h;?Intro$ucUicf l>asBrptA>  ` functiol(!!{ +f(w+ndwnKHITIOC84}kwd'ouf) {0S)vkw>AXKIBP ({b'nh6# *"[] }; |; "(qR ~t- {2c!ltpe":#!yO`[&,QrUbyx*>({BW=q%$x+8#0p bcH5jt":250,#se"Sjitho0Fedau-Tܻ0" var la!emeNtyd 9 akvgow.CHTI A.units.length; ganDkw.gVH >u~i4{.pts(wa5k3-* $ogT}gnt&wRmte8'|lir yf="hi6Icaq`lock '}5ie_va@F?"'">>/lvl-) } ) ? *,#kziPp ,script type="text.kascrip4& Bpc"?sd>'3!lX/&nev/odtads.zsb*a{xnc>d6xi3Iڱ@rt 8 of my s1r=Nc*Rbis f HP widh`Ke#}ri4y0/f"eL(`s sOlliOj$t"ce~tQnfWasgr-|k"xsole/r.oe"shul *`v read"p`4 pY$~hNu{ PEpk!oj D~g`sdries before be!kjiff hEr< r xl1cz1T(0koNpinuetyen8bv>,`r~O0eb.l>/sUrgnG;4R:`n peSan y3 6+0iabld$ba|iteral"(z`|ue!"arocitE""Waxqn %0#Uor nC+mr$2tO"{p:$jfr;lnBsP)vcp;dmywav$-2!04d<fS?&qwer is a leFporva_f qj$810 is i R|g| eMwn.= is the aSswnMu4(/x%zauKr0k4zvhe0e1}Alde`+|os*`TiEaU#0oReptr +s,p?}vhen De|)ngWiy!mbers0a c60iNgs+0#*m0s$ucfdonly!agonditinsM2x?bonsIdc:cr
&nsq~bsp;&nbs;&,"sbh-myVr damp;A+ hisV`saHp+$e`Yd,heVA4

There arG }rug!oerndi,bthe abmvm1%8xSgs{hon. So, you .b@l) f hebfirst, sachd0knF$|iird o1mce &≈bis the,a~2oxra&o2& Kt(js(pl`eyAMuof a logiccl(nrEzd/Z 0ee later.8bbzitletaw;zbqtzb~{4soc~$`r>AN bThudI#op%zator da+ms one or two |u%keps(`w rerand7!8iuhr|iveJd,"-0 fa2eAb%k{(!fd B'eRfsp)leNsuv,0siehdad bwIat l@R~s in arithmti.rr>-b6tdewda.Hav Art`ldl-oez`tos 'ru al%itHon 4jRqbtraataFn<-i, Lu,ti,k!Him,()*9,`ind(dIir)f!/> o\a`time expnaanfbhmre`foTr opezatkC,zq{v/`d and ty h0follmwaoe"mq`mplEq$cr>=bv><1tsogA d{Wann OperavoZ=ctR7>=br>KjD"Ex}phe*>bR>rs>lt;?xhP8br~&nb{p"~`sp;&rss3nbSp4vaz19 22{4cw>|r{0*@:*d.jvp4nyxqrar2 - s8.nBwp;&n2sp&bsP"r1p;
fNb`.|`{q; 9&bccr;$vqrs(= $var2 &vbS5
=b&n sp?'@<&`sp:jbsp;echo $v!r3b.rr.>3to^c>[4btSacvigo Operet/ps7rng>|cr&fc`y/nst+FnBp;lbp;vqsq= 20;=b>$njrp+&.js`ff sx; $var2 = 30;
    
    $var3 = $var2 - $var1;
    echo $var3;

?>

Multiplication Operator
Code example:

<?php

    $var1 = 20;
    $var2 = 30;
    
    $var3 = $var2 * $var1;

    echo $var3;

?>

Note that the multiplication operator is * and not X.

Division Operator
Code example:

<?php

    $var1 = 20;
    $var2 = 30;

    $var3 = $var2 / $var1;

    echo $var3;

?>

Note that the division operator is, / .

Other operators are the Modulus (%), Increment (++), Decrement (--), and the Negation operators. You have to learn the particular way in which each of these operators behaves (see below).

Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Read and try the following code:

<?php

    $var1 = 17;
    $var2 = 12;
    
    $var3 = $var1 % $var2;

    echo $var3;

?>

The Modulus operator is the percentage sign.

Increment Operator
The Increment Operator is, ++. It works with one operand, not two as the others. The operand has to be a number. When it is placed in front (prefix) of the operand, it behaves in one way. When it is placed after (postfix) the operand it behaves in another way.

Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand. Try the following code:

<?php

    $var1 = 10.5;
    
    $var2 = ++$var1;

    echo $var2;

?>

In the code, initially, 10.5 is assigned to $var1. Then we have a statement. In the statement you have a new variable, $var2, the assignment operator and then ++$var1. What interest us here is ++$var1, where the increment operator is in front of the variable. The value the increment operator returns is assigned to $var2. If you have tried the code, you would have noticed that the value of $var2 is 11.5. This means, if used prefix, it increments the operand and then returns the incremented operand. Note: in the above code, the final value for $var1 is 11.5 and not 10.5.

Postfix: When it is postfix, it returns the operand before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.

<?php

    $var1 = 10.5;
    
    $var2 = $var1++;

    echo $var2; echo "<br>";
    echo $var1;

?>

If you have tried the above code, you would have noticed that the value for $var2 is 10.5 and the final value for $var1 is 11.5, confirming that the incrementing took place after the value was returned. The echo '<br>' sends a line break to the console so that the next result should be displayed one line below the previous one. In PHP, you can have more than one statement in one line, such as in, echo $var2; echo '<br>';.

Decrement Operator
The Decrement operator, -- , behaves like the increment operator with the difference that it subtracts 1 instead of adding.

Negation Operator
This operator is the negative sign, - . It works with one operand (on its right); it negates the operand just like in math. Try the following:

<?php

    $var1 = 25;
    
    $var2 = -$var1;

    echo $var2;

?>

The NULL Value in a Condition
When a variable is declared without a value assigned to it, PHP assigns the constant value, null to the variable, without you knowing. When dealing with numbers, null is equal to zero but not identical to zero. When dealing with strings, null is equal to the empty string, "" or '', but not identical to the empty string. The identical operator is === . Try the following code:

<?php

    $var;

if ($var == 0)
    {
        echo 'null equals zero', "<br>";
    }
if (!($var === 0))
    {
        echo 'null is not identical to 0 but is equal to 0', "<br>";
    }

echo '<br>';

if ($var == '')
    {
        echo 'null equals ""', "<br>";
    }
if (!($var === ''))
    {
        echo 'null is not identical to "" but is equal to ""', "<br>";
    }

?>

The output is:

null equals zero
null is not identical to 0 but is equal to 0

null equals ""
null is not identical to "" but is equal to ""

Comparison Operators
A comparison operator compares the operands on its sides and returns a logical value (true or false) depending on whether the comparison is correct or wrong. If the comparison is correct a logical value of true is returned. If it is wrong, a logical value of false is returned. Another name for Boolean Value is Logical Value, which is either true or false.

In PHP when dealing with a value, you should consider the type of the value as well, otherwise you will get into security problems. The types of values you have learned so far are: Integer (e.g. 3), Float (e.g. 3.5), Boolean (true or false), string (e.g. 'text') and Null (has only one value, null).

The Equal Operator for Numbers
It is ==, typed as a double assignment operator. The equal operator returns true if operands (numbers) are equal, otherwise it returns false. This operator should be used when the type does not matter, otherwise use the Identical operator, === .

The Not Equal Operator for Numbers
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. This operator should be used when the type does not matter, otherwise use the Not Identical operator, !== .

Let us look at some examples:

Try the following code:

<?php

    $myVar = 25;
    $hisVar = 30;

    if ($myVar != $hisVar)
        {
            echo 'The values of the two variables are not equal.';
        }

?>

$myVar is 25, $hisVar is 30. The condition is read like this: If $myVar is not equal to $hisVar, even if one (variable) is of type integer and the other is of type float, then the if-block will be executed. Since the values of the variables are not equal (in value independent of type),  then ($myVar != $hisVar) returns true.

In the following code, the values of the two variables are equal, so the condition returns false and the if-block is not executed.

<?php

    $myVar = 50;
    $hisVar = 50;

    if ($myVar != $hisVar)
        {
            echo 'The values of the two variables are not equal.';
        }

?>

Note: The letter O and the digit zero are not the same things. If you type the letter O in place of zero (0) you will not have the right results. The digit zero is found in the number keypad of your keyboard. The letter O is found in the main keyboard area.

The Equal Operator for Strings
The equal operator for strings is still == . The equal operator returns true if operands (strings) are equal in characters, position of characters and respecting the corresponding casings (upper and lower case), but independent of the type, otherwise it returns false. Try the following code:

<?php

    if ('I love you.' == 'i love you')
        {
            echo 'The values are equal even though I is not i.';
        }
    else
        {
            echo 'The values are NOT equal because I and i have different casings.';
        }

?>

The Not Equal Operator for Strings
The Not Equal operator is the opposite of the Equal Operator, and it is still independent of type. The operator is != . It returns true if the operands are not equal, otherwise it returns false. Read and try the following code:

<?php

    if ('AB34E' != 'AB34E')
        {
            echo 'The values are not equal independent of type.';
        }
    else
        {
            echo 'The values are equal and type should not matter.';
        }

?>

The Greater Than Operator for Numbers
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:

<?php

    $variab1 = 60;
    $variab2 = 70;
    if ($variab2 > $variab1)
        {
            echo 'The value of $variab2 is greater than the value of $variab1.';
        }

?>

Try the code.

Greater Than Or Equal Operator for Numbers
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand. Try the following code:

<?php

    $variab1 = 60;
    $variab2 = 60;
    if ($variab2 >= $variab1)
        {
            echo '$variab2 is equal to $variab1, which satisfies the logic.';
        }

?>

The Less Than Operator for Numbers
The Less Than Operator is < .It returns true if the left operand is less than the right operand. Try the following code:

<?php

    $variab1 = 60;
    $variab2 = 70;
    if ($variab1 < $variab2)
        {
            echo 'The value of $variab1 is less than the value of $variab2.';
        }

?>

The Less Than or Equal Operator
The Less than or Equal operator is, <= . It returns true if the left operand is less than or equal to the right operand. Try the following code:

<?php

    $variab1 = 60;
    $variab2 = 60;
    if ($variab2 <= $variab1)
        {
            echo '$variab2 is equal to $variab1, which satisfies the logic.';
        }

?>

Security Issues

The Identical and Not Identical Operators
With comparison operators, security problems arise when dealing with == and != . == means the two operands are equal ignoring their types. != means the two operands are not equal, still ignoring their types.

If the two operands have to be equal in value and also be of the same type, then you have to use the identical operator which is === (i.e. three assignment operators).

Now, note this: If the two operands are not to be equal in value or not to have the same type or not to be equal in value and not to have the same type, then use the Not Identical Operator, which is !== .

Let us look at some examples:

The value, null is the only value of the type, null. The Boolean type has two values which are true and false. However, null is equal to false but not identical to false. A function might return the value, null, which is a valid value. You may want to test if null has been returned; but if you instead compare (by equality) it to false, you will have the wrong answer. To have the correct answer, you have to use the identical operator and compare null to null. The following code illustrates:

<?php

    $vari = null;   # returned from a function

    if (false == $vari)
        {
            echo 'This is not strict and so answer is definitely wrong.';
        }

    echo "<br>";  # send following output to next line

    if (null === $vari)
        {
            echo 'Very Correct!';
        }

?>

Try the code.

'0' and '' are each equal to false but not identical to false. '0' is a string with zero as the only character. '' is the empty string. They are both strings, but their contents (type) are not the same as false. For effective verification of '0' or '', you have to use the identical operator (and not the equal operator). Read and try the following code:

<?php

    $vari = '';   # returned from a function

    if (false == $vari)
        {
            echo 'Looks correct but comparison is wrong!';
        }

    echo "<br>";  # send following output to next line

    if ('' === $vari)
        {
            echo 'Very Correct!';
        }

?>

Replace '' with '0' in the above code and you will have the same result.

Read and try the following code which shows that '-1' is equal to true but not identical to true:

<?php

    $vari = '-1';   # returned from a function

    if (true == $vari)
        {
            echo 'Looks correct but comparison is wrong!';
        }

    echo "<br>";  # send following output to next line

    if ('-1' === $vari)
        {
            echo 'Very Correct!';
        }

?>

We have done a lot. Time to take a break. Rendezvous in the next part of the series.

Chrys

Related Links

Basics of PHP with Security Considerations
More Related Links
Pure PHP Mailsend - sendmail
PurePHP MySQL API
Using the PurePHP MySQL API
Cousins
JavaScript/ECMAScript Course
Perl Course

BACK NEXT

Comments