With connections to C and Perl, PHP has a full range of programming functionality that can be deployed in interactive web sites.  These include conditional logic and looping.  Applications can also be streamlined by the use of server side includes.

## Arithmetic Operators

Numeric variables can be programmatically manipulated with a host of operators.  Arithmetic calculations available include:

Operator Description Example Result

+

x=2
x+2

4

Subtraction

x=2
5-x

3

*

Multiplication

x=4
x*5

20

/

Division

15/5
5/2

3
2.5

%

Modulus (division remainder)

5%2
10%8
10%2

1
2
0

++

Increment

x=5
x++

x=6

Decrement

x=5
x–

x=4

Numeric variables are assigned with the =.  However they can also be assigned with the following:

Operator Example Is The Same As

=

x=y

x=y

+=

x+=y

x=x+y

-=

x-=y

x=x-y

*=

x*=y

x=x*y

/=

x/=y

x=x/y

%=

x%=y

x=x%y

A common mistake is to confuse = with double = =.  A double == is used in if/else conditional logic to check equality.

## If/Else – Conditional Logic

As with other programming languages if/else and switch logic can be used in PHP and is one of the many tools that can be deployed to make web pages more dynamic.

The syntax for an if/else statement is:

```if(condition){
//do this
}
```

That can be extended with an else to:

```if(condition){
//do this
}else{
}
```

And extended with ifelse to this:

```if(condition){
//do this
}elseif(other condition){
}else{
}
```

The conditional operators are

Operator Description Example

==

is equal to

5==8 returns false

!=

is not equal

5!=8 returns true

is greater than

5>8 returns false

is less than

5<8 returns true

>=

is greater than or equal to

5>=8 returns false

<=

is less than or equal to

5<=8 returns true

More than one condition can be applied using logical operators.

Operator Description Example

&&

and

x=6
y=3
(x < 10 && y > 1) returns true

||

or

x=6
y=3
(x==5 || y==5) returns false

!

not

x=6
y=3
!(x==y) returns true

All of the above are used to check if a condition is true.  Alternatively the following conditions can be used to check for the presence of a variable.

```if(\$somevar)
```

Returns true if the variable has a value other than 0, an empty string or NULL.

```if(isset(\$somevar))
```

The isset checks to see if the variable exists.  It returns true if the variable has any value other than NULL, including an empty string and/or 0.  The addition of an exclamation mark is used to make this negative logic.  ie !isset(\$somevar) – equates to if NOT set.

## Escaping Special Characters

The escape character in PHP is the backslash \.  This forces PHP to ignore the character that follows a backslash and treat it as mere text even if the character has some reserved meaning in PHP.  This can be used in echo and print statements when creating HTML which may require quotes around HTML attributes

```<?php
echo "<img src=\"\$image\">"
print ‘I don\’t like it when it rains’;
?>
```

## Switch/Case

Instead of long if/else/elseif statement then switch logic can be used the syntax for which is as follows:

```switch (\$somevar){
case ‘value1’:
// do this
break;
case ‘value2’:
// do this
break;
case ‘value3’:
// do this
break;
default:
// do this
}
```

If the value passed matches any of the cases then that case is executed.  The ‘break’ ensures that no more code is executed after that defined case.  If no match is made the optional default case will be executed.

```<?php
\$somevar = "value7";
switch (\$somevar){
case 'value1':
// do this
echo 'value 1';
break;
case 'value2':
// do this
echo 'value 1';
break;
case 'value3':
// do this
echo 'value 1';
break;
default:
// do this
echo "Not set";
}
?>
```

In the above example “Not set” is output as no match is found.  Switch/Cases are more efficient than multiple if/else statements particularly if you know the set of values to expect.

## While Loops

The while loop repeats a block of code as long as a tested condition remains true.

```\$count = 1;
while (\$count < 10){
print "\$count<br>";
\$count++;
} // outputs 1 to 9
```

## do … while Loops

A do while loop will always execute a least once as the condition is checked after the code block is executed.

```\$count2 = 12;
do {
print "\$count2<br>";
\$count2--;
}
while(\$count2 > 6) // outputs 12 to 6
```

## for Loops

For loops take three conditional parameters – an initial statement, a loop condition and an end-loop condition that is executed everytime the loop body is executed.

```for(\$count3=1;\$count3<10;\$count3++)
{
echo "\$count3<br>";
} // outputs 1 to 9
```

## Ternary Operator

A less well known operator but with many uses is the ternary operator that uses a question mark (?) and colon (:) in combination.  A ternary or three-value logic statement is constructed as follows:

(1) ? (2) : (3)

If ‘1’ is true then ‘2’ is returned.  If (1) is false then (3) is returned.  For example:

```<?php
\$feed = 1;
\$action = (isset(\$feed)) ? 'Go' : 'Back';
echo \$action; // outputs ‘Go’
?>
```

This is a useful way of checking for the presence of variables and assigning default values if they are missing.

A good example of use the ternary operator relates to HTML forms. Assume a HTML form element such as a radio button or checkbox needs to be checked if a variable (from a database for example) indicates it should be.

```<input type="checkbox" name="agree" value="1" <?php echo (\$agreed == 1) ? 'checked="checked"' : ''; ?>>
```

This is less verbose then creating the same logic with an `if` statement.

## User Defined Functions

There are many built-in PHP functions but you can also create your own.

```<?php
function getTemp(\$c){
\$f= (\$c*1.8)+32;
return \$f;
}
print getTemp(0); // outputs 32
?>
```

Variables in a function are local ie are valid only in the scope of the function – unless declared as global.  Note that global variables are declared in functions not outside as in other languages.  The result of the function is determined by the ‘return’ value.  When using a global variable there is no need for the return as below.

```<?php
function cubeThis2(\$value){
global \$cube;
\$cube = \$value * \$value * \$value;
}
\$sort = 3;
cubeThis2(\$sort);
print "\$cube";
?>
```

However it is not good programming practice to overuse the global variables so the following would be more preferable.

```<?php
function cubeThis(\$value){
\$cube = \$value * \$value * \$value;
return \$cube;
}
?>
```

## Server Side Includes

One of the benefits of server side scripts is the ability to use server side includes.  That is to place reusable code in a single file and then reference it from other files thus reducing site maintenance.”

```<?php
require "mystuff.php";
?>
```

Paths to the include file can be both relative and absolute.

```<?php
require "data/includes/mystuff.php";
?>
```

The include command can also be used and works exactly the same as require but produces a warning rather than a fatal error.

Both include and require have variants include_once and require_once.  When calling an include with these variants if the code from a file has already been included, it will not be included again.

Tip:  The INC file extension is often used to identify a file as an include.  However, these files will not be read by the PHP interpreter which can lead to the code on the pages been exposed via a browser.  Therefore use the double extension myinclude.inc.php.  This identifies the file as an include for the developer but ensures it is interpreted before been parsed by the browser.

```<?php