Marc Fraser | Web Design and Development Blog A Web Design and Development Blog by Marc Fraser. Marc Fraser is a student, situated in Central Scotland.

10Oct/092

PHP Include

(from Full Article)

The include command can save you a lot of time as you can create a header, place it in a file and just include that file on every page and the header will be shown. This will also mean that you do not need to edit every page every time you need to edit your header!

Basically the PHP Include command takes the file name and inserts the contents of the file into the script.

To illustrate this, I will use an example:

  1. Create a HTML file called Menu.html and insert the following code into it:
<ul>

<li>Home</li>

<li>Articles</li>

<li>Tutorial</li>

</ul>
  1. Now create a menu_example.php PHP file (in the same directory as Menu.html) and insert:
<?php

include(‘Menu.html’);

?>
  1. Navigate to menu_example.php in your browser and run it – you should see:
Shows the menu that is being included.
Menu Included.
Tagged as: 2 Comments
10Oct/090

PHP IF Statement

(from Full Article)

In PHP IF statements are extensively used.  It allows you to show the user data depending on certain conditions. The best way to describe it is with the use of an example: You want to build a dashboard with items you do not want all employees to see, this is where IF statements come in handy – you can check that they are a certain person or a member of a certain group and if this is true the employee can see the item, if it is false (the employee isn’t in the necessary group) then the employee shouldn’t see the items.

We will use Comparison Operators in the example below;

<?php

$isManagement = false;

if($isManagement == true) {

// Show the data that you wish only management to see.

} else {

// You are not management and so cannot see this data.

}

?>

The above example uses comments to show how the if statement works in terms of the example farther above. $isManagement will come from the employees record.

Within the IF() statement we are comparing to see if $isManagement is true, within the first parenthesis ({}) is where the PHP code will go if the result is true. The else statement is included to catch all other occurrences apart from the true occurrences. Please see the PHP Manual for another example.

10Oct/090

PHP Operators

(from Full Article)

Here are most types of PHP Operators: Assignment Operators, Arithmetic Operators, Comparison Operators, and String Operators.

Assignment Operators

We have already met this type. This is where we set a variable equal to something or make a variable equal to another variable.

Arithmetic Operators

English Operator Example
Addition + 1+1
Subtraction - 5-3
Multiplication * 5*2
Division / 10/2

An example of the use of Arithmetic operators is shown below:

<?php

$add = 1+1;

$subtract = 5-3;

$multiply = 5*2;

$divide = 10/2;

print “Addition Answer: ” . $add . “<br>”;

print “Subtraction Answer: ” . $subtract. “<br>”;

print “Multiplication Answer: ” . $multiply. “<br>”;

print “Addition Answer: ” . $divide. “<br>”;

?>

The output of the above code is:

Addition Answer: 2
Subtraction Answer: 2
Multiplication Answer: 10
Addition Answer: 5

This showing that the server is carrying out the calculations, using our arithmetic operators. As well as the above, you could also use these operators in conjunction with each other like so:

<?php

$quantity = 4;

$value = 5;

$total = $quantity * $value;

print $total;

?>

The output from the above is 20.

Comparison Operators

Comparison Operators are used to check the relationship between variables/values. These are used inside statements (such as if statements) to check whether the result is true or false. Listed are some PHP comparison operators:

We will assume, for this example, that $a = 2 and $b = 3.

English Operator Example Result
Equal to == $a == $b False
Not equal to != $a != $b True
Less than < $a < $b True
Greater than > $a > $b False
Less than or equal to <= $a <= $b True
Greater than or equal to >= $a >= $b False

Please note the two equal signs in the equal to example – this is a common beginners’ mistake.

We will go into further examples of Comparison Operators in the next section on PHP If Statements.

String Operators

We have already met String Operators, please read prior section named Concatenation.

10Oct/091

PHP Concatenation

(from Full Article)

We can display our variables in a meaningful fashion with the use of concatenation. Concatenation makes use of the period (.). Please see the example below which is an example of affixing a value to the end of the variable:

<?php

$name = “Marc”;

$name .= “Fraser”;

?>

Basically what ‘.=’ is doing is saying use the current value of $name and add “Fraser” to the end of it. If I were to now print $name, it would output “MarcFraser”.

We can also use concatenation to create readable sentences:

<?php

$name = “ Marc Fraser”;

$age = 16;

print “We would like to welcome you,” . $name . “, age” . $age . “ to our website!”;

?>

This will output “We would like to welcome you, Marc Fraser, age 16 to our website!”. To do this, we close the string (close quotation mark) and add our period, which tells the server that we wish to begin parsing PHP again. When we want to stop parsing PHP we add a period and then begin our string again (quotation mark).

10Oct/090

PHP Variables

(from Full Article)

Variables are important to PHP. A variable is a way of storing a value, such as text (“Hello”), numeric values (such as the integer value of 1) and also Boolean values (true and false). A variable can be re-used throughout your code instead of typing out the value over and over again. Variables will also not be new to you if you’ve had any experience of Algebra in Mathematics.

You define a variable in PHP like so:

<?php

$name = “Marc Fraser”;

$age = 16;

?>

The first things you should notice is the dollar sign ($). In PHP this signifies that the item is a variable, I can name a variable whatever I wish such as $a, $b, $c, as long as I do not omit it – otherwise syntax errors will occur. At this time, you should also note that variables are case sensitive, therefore $a is different in the eyes of PHP than $A.

Once our data is stored in a variable, we can perform operations to it, such as print, string manipulation and if the variable is an integer, calculations can be done. Below is how to print a variable:

<?php

$name = “Marc Fraser”;

$age = 16;

print $name;

print $age;

?>

The above will output “Marc Fraser16”.

Variable naming conventions:

  • PHP Variables may only start with a letter or underscore (_).
  • Spaces are not permitted in PHP variables and so underscores, or capitalization should be used – such as: $this_variable, $thisVariable.
  • Variables may only comprise of alphanumeric characters.

Bad Behavior has blocked 66 access attempts in the last 7 days.