Archive

Posts Tagged ‘Operators’

PHP – Basic

February 8th, 2009 No comments

PHP is a server-side scripting language which allow for the creation dynamic and interactive webpages, standing for PHP: Hypertext Preprocessor

It is the widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code of a page.

PHP is often used together with the Apache web server on various operating systems. It also supports ISAPI and can be used with Microsoft’s IIS on Windows.

This article will go through some useful PHP syntax and uses.

Basic Syntax

Basic Syntax included in a PHP page and how in integrates with HTML

A piece of PHP code will always start with the tag <?php and end with ?>

Using the echo ” ” ; tag displays standard HTML that the browser will display. To echo multiple strings of variables (explained later) use a dot . <HTML> <BODY> <?php echo “<h1> Hello World </h1>”; echo “Hello” . “World”; ?> </BODY> </HTML>

Comments

Adding comments in PHP is just as important as any other programming language. within PHP there are two different methods for adding comments, through the // syntax for a single line, or to add a block of comment use /* to start, and */ to finish the block

<HTML>
<BODY>
<?php
//Single Comment
/*
Start of comment block
Still commenting
*/
?>
</BODY>
</HTML>

Variables

Variables are not typed in PHP, when referencing variables in PHP they just start with the $ symbol.

The snippet of code assigns a variable, then uses the echo command to output its value

<HTML>
<BODY>
<?php
$var1=”Hello World”;
echo $var1;
?>
</BODY>
</HTML>

Operators

PHP has the several standard operators which can be used, see table below

PHP Operators
Operator Action
+ Add
- Subtract
* Multiply
/ Divide
% Modulus
++ Increment
Decrement
= Equals
== Is equal to
!= Is NOT equal to
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
&& and
|| or
! not

Conditional Statements

Conditional statements help make PHP a dynamic language, being able to put if statements into webpages allows for the generation of dynamic HTML. The common statements are described below:

If statement

The most common statement, the syntax for this is shown below

<HTML>
<BODY>
<?php
if ($x==$y)
echo $x . ” does equal ” . $y;
elseif ($x < $y)
echo $x . ” is less than ” . $y;
else
echo $x . ” does not equal ” . $y;
?>
</BODY>
</HTML>

Looping Statements

Loops are used to save repetition of code, instead a piece of code is used once and told to be repeated a number of times, or until a condition is met.

While statement

The While statement should be used when the number of iterations is unknown, the syntax is shown below

<HTML>
<BODY>
<?php
while ($x != $y)
{
$x++;
echo “X is ” . $x;
}
?>
</BODY>
</HTML>

The other method of writing a While loop is the dowhile loop. In this example the code is always run once before the do statement is reached.

<HTML>
<BODY>
<?php
do
{
$x–;
echo “X is ” . $x;
}
while ($x > 15);
?>
</BODY>
</HTML>

For Loop

This statement is used when the number of repetitions required is known.

<HTML>
<BODY>
<?php
for ($i=1; $i<=5; $i++)
{
echo “i is ” . $i;
}
?>
</BODY>
</HTML>

Foreach Loop

This is very similar to the For Loop above, however in this case the loop is done through an array which is passed into the loop.

<HTML>
<BODY>
<?php
$arr1=array(“dayone”, “daytwo”, “daythree”);
foreach ($arr1 as $value)
{
echo “Value is ” . $value;
}
?>
</BODY>
</HTML>

Arrays

Arrays are very useful data types which are commonly under used. an array can consist of any type of data that is required, is provides a better way of storing the data and opens up easier ways of using the data when in this format.

There are three different types of array:

  • Numeric Arrays – array where there is a number that references the position in the array when being accessed
  • Associative arrays – array where values are used to reference the location within the array
  • Multidimensional arrays – Simply an array with has more than one array within it

Numeric Array

Two different methods of creating these types of arrays, both yield the same result

<HTML>
<BODY>
<?php
$arr1=array(“dayone”, “daytwo”, “daythree”);

$arr2[0]=vaule1;
$arr2[1]=vaule2;
$arr2[2]=vaule3;

//Referencing the array is done the same way
echo $arr1[0];
echo $arr2[1];
?>
</BODY>
</HTML>

Associative Array

This array type does not use standard numbering that numeric array does (0, 1, 2, 3…), here the array entries are referenced by user defined values. Again there are two different ways of creating this array type.

<HTML>
<BODY>
<?php
$arr1=array(“dayone”=>28, “daytwo”=>25, “daythree”=>20);

$arr2['monday']= “11″;
$arr2['tuesday']=”12″;
$arr2['wednesday']=”13″;

//Referencing the array is done the same way
echo $arr1['dayone'];
echo $arr2['tuesday'];
?>
</BODY>
</HTML>

Multidimensional Array

The Multidimensional array is the most complicated of them all, when implemented a grid like array is formed. When implementing a multidimensional array the sub array of each array item does not have to be the same size, as shown below.

<HTML>
<BODY>
<?php
$streets = array(
“Highstreet”=>array(
“No1″,
“No5″,
“No10″,
),
“MiddleStreet”=>array(
“No100″
),
“LowStreet”=>array(
“No33″,
“No34″,
“No35″
)
);
?>
</BODY>
</HTML>

Forms

With the use of HTML forms and PHP, information can be passed between pages easily through using the different methods available.

A basic HTML form looks like the following:

<HTML>
<BODY>
<FORM action=”page2.php” method=”POST”>
Enter value1: <input type=”text” name=”value1″ />
Enter value2: <input type=”text” name=”value2″ />
<input type=”submit” />
</FORM>
</BODY>
</HTML>

Once submitted to page2.php the values can be shown with the following code:

<HTML>
<BODY>
Hello World<br>
<?php echo $_POST["value1"]; ?><br>
<?php echo $_POST["value2"]; ?>
</BODY>
</HTML>

The syntax $_POST changes to $_GET depending on the form method. The final piece of syntax is $_REQUEST, this contains values for $_GET, $_POST and $_COOKIE (discussed later)

Form Get method displays the data transferred between pages in the address bar, the advantage of this is the new page can be book-marked and each time opened the same data will appear. The disadvantage the limit of 100 characters per variable.

Form Post method does not display the data transferred between pages in the address bar, instead the data is piped directly between pages. in this case the new page can’t be book-marked successfully, however the variable size limit is not enforced.
Many thanks to the tutorials where this information came from, W3Schools & PHP