Monday, September 29, 2008
The strcmp() function
Syntax: strmp(string1,string2);
This function returns 0 if both the strings are equal.
It returns a value less than zero, if string1 is less than string2.
It returns an integer greater than zero if string1 is greater than string2.
Example input:
$str1 = "Hello";
$str2 = "Halo";
echo strcmp($str1,$str2);
Example output:
1
The implode() function
Syntax: implode([separator, ]array);
separator - this parameter specifies the character or characters to be put between the array elements when they are glued together into a string. The default value is an empty space.
array – the input array
Example input:
$employee = array('This', 'is','John.');
echo implode(" ",$employee);
Example output:
This is John.
The explode() function
Syntax: explode(separator,string[,limit]);
separator – the character or characters at which the string should be split.
string – the input string
limit – the maximum number of array elements you allow the string to be split into.
Example inputs:
1.
$str= "This is a simple program";
print_r(explode(" ",$str));
2.
$str= "This is a simple program";
print_r(explode(" ",$str,3));
Carefully note the difference in output between the two examples.
Example outputs:
1.
Array ( [0] => This [1] => is [2] => a [3] => simple [4] => program )
2.
Array ( [0] => This [1] => is [2] => a simple program )
Tuesday, September 23, 2008
The substr_replace() function
Syntax: substr_replace(string,sub-string,start[,length]);
sub-string – the string that replaces all or portion of the larger string.
start – the position in the original string from where the replacement should start.
If the start parameter is positive, the position is calculated from the beginning of the string.
If the start parameter is negative, the position is calculated from the end of the string.
A start parameter with the value zero would mean that the replacement should start from the first character of the string.
length – the number of characters that should be replaced.
A positive value for the length parameter indicates the number of characters to be replaced.
A negative value for the length parameter indicates the number of characters to be left at the end of the string.
echo $str1;
echo "
";
echo $str2 = substr_replace($str1,"Her",0,3);
echo "
";
echo $str3 = substr_replace($str2,"Marie",-5);
echo "
";
echo substr_replace($str3," ",0,-5);
The substr_count() function
Syntax: substr_count(string,sub-string[,start,length]);
sub-string- the string to be searched for
start – that position in the larger string from where the search should begin
length – length of the search from the starting position
If start and length parameters are not specified, substr_count() carries out search in the entire string.
echo $str1;
echo "
";
echo substr_count($str1,"PHP",3,20);
echo "
";
echo substr_count($str1,"PHP",3,10);
echo "
";
echo substr_count($str1,"i");
The strtok() function
Syntax: strtok(string,split);
split – the split character
echo $str1;
echo "
";
echo $str2 = strtok($str1," ");
echo "
";
echo strtok(" ");
echo "
";
echo strtok(" ");
Friday, September 19, 2008
The substr() function
Syntax: substr(string,start[,length]);
start – the starting position from where a sub-string should be extracted.
length – the number of characters the extracted string should contain.
Both start and length can also contain negative values.
A negative start parameter indicates that the position should be determined from the end of the string.
A negative length parameter would extract the certain number of characters from the end of the string.
If the length parameter is not specified, the rest of the string from the specified position would be returned.
Example inputs:
1.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr($str1,17,3);
Here,the function returns the three characters after position 17 in $str1.
2.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr($str1,-17);
3.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr($str1,-17,-3);
Example outputs:
1.
This is a simple PHP program PHP
2.
This is a simple PHP program imple PHP program
3.
This is a simple PHP program imple PHP prog
The strstr() function
If our search string is not found, this function returns FALSE.
Syntax: strstr(string,search string);
search string – the sub-string that is to be searched in the larger string.
Example input:
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo strstr($str1,"PHP");
echo "
";
echo strstr($str1,"p");
Example output:
This is a simple PHP program PHP program ple PHP program
Thursday, September 18, 2008
The wordwrap() function
Syntax: wordwrap(string[,length,break,cut]);
length – the number of characters after which the string is wrapped. The default length is 75.
break – the character to be used as a separator. “\n” is the default.
cut – If this is TRUE, words longer than the specified length will also be wrapped.
If this value is FALSE, words longer than the specified length will not be wrapped. FALSE is the default value.
Example inputs:
1.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
$str2 = wordwrap($str1,2,"
",true);
echo $str2;
2.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
$str2 = wordwrap($str1,2,"
");
echo $str2;
In the second example, we have not mentioned any value for the cut parameter. The default value is FALSE and hence, words in the string that are longer than the specified length(here 2) will not be wrapped.
Example outputs:
1.
This is a simple PHP program Th is is a si mp le PH P pr og ra m
2.
This is a simple PHP program This is a simple PHP program
The addcslashes() function
Syntax: addcslashes(string,characters);
characters – the characters before which a backslash is to be added.
Example inputs:
1.
$str1=" my program";
echo $str1;
echo "
";
echo addcslashes($str1,"r");
echo "
";
echo addcslashes($str1,"r,m");
2.
$str1=" my program";
echo $str1;
echo "
";
echo addcslashes($str1,"a...p");
In this case, a backslash is added before every character that falls between a and p in the alphabetical sequence.
3.
$str1=" 121232342434";
echo $str1;
echo "
";
echo addcslashes($str1,"1...3");
Example outputs:
1.
my program my p\rog\ram \my p\rog\ra\m
2.
my program \my \pr\o\gr\a\m
3.
121232342434 \1\2\1\2\3\2\34\24\34
Tuesday, September 16, 2008
The strtouper() function
Syntax: strtoupper(string);
Example input:
$str1=" my PrOgRam";
echo $str1;
echo "
";
echo strtoupper($str1);
Example output:
my PrOgRam
MY PROGRAM
The strtolower() function
This function returns the lowercase version of a given string.
Syntax: strtolower(string);
Example input:
$str1=" MY PROGRAM";
echo strtolower($str1);
echo "
";
echo $str1;
Example output:
my program
MY PROGRAM
Monday, September 15, 2008
The chunk_split() function
Syntax: chunk_split(string,length,seperator);
length – the number of characters each smaller strings should contain.
seperator – this character will be inserted after every smaller string.
echo chunk_split($str1,2,"@");
Sunday, September 14, 2008
The chr() function
Example inputs:
echo chr(100);
echo "
";
echo chr(101);
echo "
";
echo chr(0101);
echo "
";
echo chr(0XFF);
The strlen() function
Syntax: strlen(string);
Example inputs:
$str1= "A simple PHP program";
echo strlen($str1);
Saturday, September 13, 2008
The strrev() function
Syntax: strrev(string);
Example inputs:
$str1= "A simple PHP program";
echo $str1."
";
$str2 = strrev($str1);
echo $str2."
";
echo $str1."
";
echo strrev($str1);
The chop() function
The ltrim() function
echo $str1;
echo "
";
echo ltrim($str1);
trim() function
Syntax: trim(string, [list of characters]);
string- the string in which the predefined characters are to be removed.
list of characters.
\0 for NULL
\t for TAB
\n for new line
“ ” for whitespace
If the list of characters are not specified, all pre-defined characters would be removed.
$first_name = “James ”;
echo $first_name.”
”;
trim($first_name);
echo first_name;
In the output, the second echo function prints $first_name that has one less space.
2.
$name = “John
”;
echo $name.”
”;
trim($name);
echo $name;
In this case, the second time $name is printed, the new line character is absent.
Concatenation
Example inputs
$name=”Rick”;
echo “My name is “.$name;
2.
echo “My name is “.”Rick”;
3.
$str1 = “My name is”;
$str2 = “Rick”;
$str3 = $str1.$str2;
Example outputs
1.
My name is Rick.
2.
My name is Rick
3.
My name is Rick
Thursday, September 11, 2008
The print_r() function
However, to print an array as such, we must use the print_r() function.
$expression the expression to be printed.
$return
if this parameter is FALSE, the output would be printed. FALSE is the default value.
$employee = array(array('John', 223,42),
array('Daisy',443,50),
array('Mathew',990,37)
);
print_r($employee);
echo "
";
print_r($sum = 100 + 300);?
2.?
$employee = array(array('John', 223,42),
array('Daisy',443,50),
array('Mathew',990,37)
);
print_r($employee,true);
echo "
";
echo $str;
Array ( [0] => Array ( [0] => John [1] => 223 [2] => 42 ) [1] => Array ( [0] => Daisy [1] => 443 [2] => 50 ) [2] => Array ( [0] => Mathew [1] => 990 [2] => 37 ) ) ?400??
Wednesday, September 10, 2008
Data types in PHP
Numeric data type
String data type
Array data type
1. Integer Data Type
Integer data types contain whole number values. Integer values range from -32767 to 32768.
Double contains floating point numbers.
A String data type is used to contain textual information or letters. The value is assigned within quotes as shown below.
Array Data Type
An array is a variable that contains several variables of the same data type.
Each element in an array can be accessed using an id or index.
Numeric arrays
Associative arrays
Multi-dimensional arrays
Elements in a numeric array are stored with a numeric index.
We could either assign the index manually or let PHP to assign it.
The IDs could be used in your PHP code to access the elements in the array.
Associative Arrays
In associative arrays, each ID is associated with a value. That is, you can use the values themselves as keys and assign values to them.
In multi-dimensional arrays, an array element can have another array as its value and these values can in turn have arrays as their values.
Multi-dimensional arrays can be two-dimensional or three-dimensional.
While, two-dimensional arrays can be perceived as having a row and a column, three-dimensional arrays have one more dimension. That is, a three dimensional array has many two-dimensional arrays.
Assigning indices manually
$student[1] = ?Maria?;
$student[2] = ?John?;
$student[3] = ?Mathew?;?
2.
Assigning IDs automatically?
$students = {?Maria?,John?,?Mathew?};?
In this case, the indices are automatically assigned by PHP.??
3.
echo $students[0];
echo ?
?;
echo $students[2];????
Let us use an associative array to store the names and salaries of a few employees:?
One way of creating an associative array:
$salary = {?James?=>2000, ?Maira?=>5000, ?Mathew?=>3000};?
You can also create an associative array like this:?
$salary[?James?] = ?2000;
$salary[?Maria?] = ?5000;
$salary[?Mathew?] = ?3000?;
echo ?James?s salary:?;
echo ?
?;
echo $salary[?James?];???
Let?s assume that you need to store the names, IDs and ages of a few employees in an array. You can do that by using a two-dimensional array.
$employee = array{array(?John?, 223,42),
array(?Daisy?,443,50),
array(?Mathew?,990,37)
};?
You can easily access the values of the arrays as follows:?
echo ?Name:?;
echo $employee[0][0];
echo ?
?;
echo ?ID:?;
echo $employee[0][1];
echo ?
?;
echo ?Age:?;
echo $employee[0][2];?
A multi-dimensional array can also be an array of associative arrays.
That is, the above array can also be created like this:?
$employee = array{array(Name=>?John?,???????????????????? ID=>223,Age=>42),
array(Name=>?Daisy?,???????????????????? ID=>443,Age=>50),
array(Name=>?Mathew?,???????????????????? ID=>990,Age=>37)
}?
In this case, you may access the
array values like this:?
echo ?Name:?;
echo $employee[2][?Name?];
echo ?
?;
echo ?ID:?;
echo $employee[2][?ID?];
echo ?
?;
echo ?Age:?
echo $employee[2][?Age?];
To understand three-dimensional arrays better, let us assume that we need to store the details of employees in different departments in a single array.
We could do that as follows:?
$shop = array
(
array(
array("John", 223, 42),
array("daisy", 443, 50),
array("Mathew", 990, 37)
??????? ),
array(
array("Smith", 390, 45),
array("Clive", 230, 52),
array("Stephen", 900, 32)
???????? ),
array(array("Chris", 553, 35),
array("Natasha", 725, 45),
array("Jack", 617, 47)
??????? )
);?
Each element of the above three-dimensional array is a two-dimensional array. So you could store all employees under a category in a two-dimensional array ie., in an element of the three-dimensional array.?
The array elements could be accessed as follows:
echo $employee[0][0][0];
echo ?
?;
echo $employee[0][0][1];
echo ?
?;
echo $employee[0][0][2];
Sunday, September 7, 2008
The print Function
Syntax: print variablename;
Example inputs:
1.
print “smith”;
2.
$name = “Jones”;
print $name;
3.
$name=”Natasha”;
print “My name is $name”;
print “
”
print “What is yours?”;
Example outputs:
1.
Smith
2.
Jones
3.
My name is Natasha
What is yours?
Some PHP Exercises
What is the difference between using single quotes and double quotes in the echo function?
Friday, September 5, 2008
The echo function
Syntax: echo variable-name;
echo “smith”;
2.
$name = “Jones”;
echo $name;
3.
$name=”Natasha”;
echo “My name is $name”;
echo “
”
echo “What is yours?”;
4.
$name = “Robin”;
echo ‘My name is $name’
5.
echo ‘My’,’name’,’is’,’John’;
Smith
2.
Jones
3.
My name is Natasha
What is yours?
4.
My name is $name
5.
My name is John
Some PHP Exercises
What are the rules for naming a PHP variable?
Can I start a PHP variable name with the # symbol?
Should we always declare a variable in PHP specifying its data type?
Is it true that all statements in PHP should end with a comma?
Thursday, September 4, 2008
Variables in PHP
Variables are identifiers to the memory location to store data. A PHP program can contain as many variables as you’d want.
Variable names in PHP begin with a dollar sign "$" and the values are assigned using the “=” operator.
Following the $ sign should be an alphabet or an underscore.
A variable name can contain any number of alphabets, numbers or underscores.
A PHP variable name cannot contain special characters like *, +, @, # etc.
In PHP we need not specify the variable type, as PHP takes the data type of the assigned value.
1. $1name is an invalid variable name as it starts with a number.
2. $_name or $name1 are, however, valid PHP variable names.
3.
$Name = "David";
$Age = 16;
In the above case, we assign values to the variable without mentioning their type. PHP automatically recognizes $Age as a numeric variable and $Name as a string.
Some PHP Exercises
Can I have HTML tags in a PHP file?
Is it true that all PHP statements should end with a comma?
Wednesday, September 3, 2008
A PHP File
A PHP program could be written with a simple text editor such as notepad. In order to develop web pages that look better, web development applications such as Macromedia Dreamweaver could also be used.
A PHP file would contain PHP statements, functions etc. as well as HTML tags and scripts. However, there could be PHP files that contain no HTML.
PHP files are usually saved with the extension, ".php”.
All statements in PHP end with a semicolon.
Some PHP Exercises
What was PHP/FI?
For what purpose was PHP/FI first developed?
Who developed PHP 3.0?
Is PHP4.0 currently under active development?
Was PHP 3.0 object-oriented?
Is PHP 5.0 object-oriented?
Tuesday, September 2, 2008
History of PHP
Rasmus later incorporated a larger C implementation to PHP/FI when more functionality was required. He then released the source code of his product so that everybody could use it and anybody could fix the bugs in it.
By 1997, Rasmus incorporated more C implementation and thousands of users started using PHP/FI, with some of them even contributing small pieces of code.
Later in 1997, Andi Gutmans and Zeev Suraski claimed that PHP/FI was grossly inadequate for developing an eCommerce application for a University project they were working on.
The duo then began to completely re-write PHP/FI and several others contributed many extension modules to the write-up to come up with what Andi and Zeev called PHP3.0.
After nine-months of testing, PHP3.0 was officially released in June 1998. Andi and Zeev also went on to announce PHP3.0 as the official successor of PHP/FI. PHP 3.0 was object-oriented and also provided a more consistent syntax structure.
Despite the huge success of PHP3.0, Andi and Zeev continued to re-write PHP’s core to improve PHP’s performance on complex applications. This was made possible with their newly developed “Zend Engine”.
In 1999, Andi Gutmans and Zeev Suraski together founded California-headquartered, Zend Technologies Ltd.
Officially released in May, 2000, PHP4.0 was based on the “Zend Engine”. PHP 4.0 supported more web servers than its predecessors and provided a more secure way of handling user input. PHP 4.0 is no longer under active development.
PHP 5 was released in July 2004 and had many enhancements including improved support for object-oriented programming.
PHP 6 is currently under development and is likely to be released with some major changes.
Some PHP Exercises
What is an open-source programming language?
What does a server-side scripting language mean?
What are the inputs and outputs of PHP?
List a few databases PHP supports?