Search
Sponsors

Archive for the ‘PHP’ Category

Variable Tutor part 2

Thursday, February 21st, 2008

Variable Tutor part 2

In part 2 we are going to deal with Data Types and Type Casting amongst other things.

Data Types

These are the data types which uses .

  • string ( text )
  • integer (numeric)
  • double(numeric)
  • array
  • object
  • unknown type

As you can see there are not as many data type are there are in some other languages , this in fact is a help to many a programmer . As a programmer you do not worry about setting these data types , PHP does it for you.

Strings

This is the data type that stores text , this can be single characters , words and even complete sentences . How many strings do you think we have below .

$mywages = “not much”;
$intloan = “3000″;

If you said one you are wrong , if you said two give yourself a pat on the back . remember we enclosed the number with quotation marks so PHP treats this as text information .

Concatenation

This means adding one string to another . If you are coming from another programming language this may (will) catch you out from time to time . In PHP we use the period ( . ) to concatenate strings.For example

$firstname = “Bill”;
$secondname = “Gates”;
echo $firstname . $secondname;

would print on the screen BillGates

Numeric Values

There are 2 different types of numerical data types in PHP , integers and doubles. Integers are basicly whole numbers and doubles are fractional (floating point ) numbers . Here are some examples .

$firstint = 9;
$firstdouble = 23.67;
$secondint = - 76;
$seconddouble = -1239.7432;

The values these types can hold is platform specific but for example the range for an integer is commonly -32768 to 32767 on Windows machines.

Type Casting

As you are aware PHP automatically decides what data type but you do have the option of specifying this yourself . Here is how you can do this .

$luckynumber = 7;
$lucknumber = (string) $luckynumber;

This would give us a string rather than the integer we had previously.

Using gettype and settype functions

These are two functions provided with PHP which help us to determine the data type and set the data type respectively , lets look at examples of both . Firstly settype which will display the data type of the variable

<?php
$myname = “Iain”;
echo gettype($myname);
?>

string
Now for settype , this allows you to set the data type . The first parameter is the actual variable , the second is the data type you require) . In this example we set the data type and then use gettype to show it has changed.

<?php
$wages = 1000;
settype($wages, “string”);
echo gettype($wages);
?>

string

variables in PHP part 1

Thursday, February 21st, 2008

variables in PHP part 1
Variables

A variable is an area of memory which is set aside to store information , this is assigned an identifier by the programmer . You can recognise variables in PHP because they are prefixed with the dollar ( $ ) sign . To assign a variable you use the assignment operator ( = ) . Here is an example of this.

$name = “iain hendry”;
$intDaysInWeek = 7;

In the first example the variable identifier in this example is $name and the string value “iain hendry” has been assigned to it . In the second example the variable identifier is $intDaysInWeek and the number 7 is assigned to it . Note that in the number example we do not surround the variable with quotes in this way PHP treats this as a numeric value but if we had put quotes round it then PHP would have treated it as a string.

Variable Naming

There are some guidelines to follow for naming variables in PHP and these are as follows .
1. All variable names must begin with a letter or underscore character .
2 . The name of the variable can be made up of numbers , letters , underscores but you cant use charcters like + , - , & , £ , * , etc
One important thing to note if you are coming from another programming language there is no size limit for variables .

Case Sensitivity

One thing that causes many hours of hair pulling and anguish is case sensitivity , PHP is case sensitive (some languages are not) . Here is an example of what I mean.

<?php
$myname = “Iain Hendry”;
echo $Myname”;
?>

Now we all know what we want to do , declare a variable , assign it the value of “Iain Hendry” and then print this on the screen but in this example we have mis-spelt the variable name , when run the following error is displayed on the screen.

Warning: Undefined variable: Myname in D:\testsample.php on line 3

Example

Using your favourite HTML editor enter the following

<?php
$url = “http://www.myscripting.com”;
$rank = 1;
echo “Our favourite site is “.$url;
echo “<br>”;
echo “It is number “.$rank;
?>

here is the output you should get

Our favourite site is http://www.myscripting.com
It is number 1

Adding previous and next to a file

Thursday, February 21st, 2008

by Hal

The following script will break a file up into x sections and show each one with the aid of next and pervious links. To test this script, create a file called ‘file.txt’ and enter test1|test2|test3 10 times on 10 lines. The script should show only the first 2 lines at first, click next would show the next 2 etc.

<?php

$page=$_GET['page'];

/*begins at page one unless another page number is set*/
if(!isset($HTTP_GET_VARS['page']))
{
$page = 1;
}

/*file address put into an array*/
$array = file(”file.txt”);

$s=sizeof($array);

/*number of lines to show per page*/
$show_per_page = 2;

$start = ($page * $show_per_page) - $show_per_page;

/*cutting the array up into sections (pages)*/
$slice = array_slice($array, $start, $show_per_page);

$end=$show_per_page*$page;

/*creating the next and pervious links. If next or previous doesn’t exist, no link is shown*/
if ($start != ‘0′) {
$new_page=$page-1;
$previous=”<a href=’test.php?page=$new_page’>Previous</a>”;
}
else {
$previous=”";
}
if ($end < $s) {
$new_page1=$page+1;
$next=”<a href=’test.php?page=$new_page1′>Next</a>”;
}
else {
$next=”";
}

/*decorating the links*/
echo “<table width=100% align=’center’><tr><td width=50%>$previous</td><td align=right width=50%>$next</td></tr></table>”;

/*the spacer which seperates the sections*/
$break=”|”;

/*looping through and echoing the file parts one by one*/
foreach ($slice as $thisline) {
list($one,$two,$three)=explode($break,$thisline);
echo”$one $two $three”;
}

?>

This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License.

Translate