Sunday, October 26, 2008

The convert_cyr_string() function

This function converts a string in one Cyrillic character set to another.

syntax: convert_cyr_string(string,from,to);

from – the character set from which the string should be converted.
to – the character set to which the string should be converted.

The from and two parameters can hold the following any of the following values.

k - koi8-r
w - windows-1251
i - iso8859-5
a - x-cp866
d - x-cp866
m - x-mac-cyrillic


Example input:

$str = "This is Vince æøå";
echo $str;
echo "
";
echo convert_cyr_string($str,'w','a');

Example output:

This is Vince æøå
This is Vince ¦è¥

Friday, October 17, 2008

The str_split() function

This function splits a string into an array of strings.

syntax: str_split(string[,length]);

string – the input string
length – the number of characters each element in the output array must hold. The default value is 1.

Example input:

$str1="This is a simple program number";
print_r(str_split($str1,5));

Example output:

Array ( [0] => This [1] => is a [2] => simpl [3] => e pro [4] => gram [5] => numbe [6] => r )

Thursday, October 16, 2008

The str_rot13() function

This function performs ROT13 encoding on a string.

Syntax: str_rot13(string);

Example input:

$str1="This is a simple program number 1";
$str2= str_rot13($str1);
echo $str2;
echo "
";
echo $str3= str_rot13($str2);

Example output:

Guvf vf n fvzcyr cebtenz ahzore 1
This is a simple program number 1

What is ROT13?

A ROT13 enoding algorithm moves every character in a string 13 places forward. Numbers and special characters, however, are untouched.


Another interesting fact about the ROT13 algorithm is that the same algorithm could be used for encoding as well as decoding as you’d understand when you analyze the example.

myLot User Profile

Friday, October 10, 2008

The str_pad() function

This string could be used to pad a string to a new length.

Syntax: str_pad(string,length[,pad-string,pad-type]);

string – the input string

length – the length of the padded string. If this value is lesser than length of the actual string, no effect will take place

pad-string – the string to be used for padding. If this value is not specified, white spaces will be added as padding.

pad-type –
This parameter can have the following values:

STR_PAD_RIGHT: This is the default value and adds padding to the right of the string.
STR_PAD_LEFT: This value adds padding to the left end of the string.
STR_PAD_BOTH : This value adds padding to both the ends. If the number of characters to be added is not even, the right side is added an extra character.

Example input:

$str1="This is a simple program";
echo str_pad($str1,40,"*",STR_PAD_BOTH);

Example output:

********This is a simple program********

Wednesday, October 1, 2008

The str_shuffle() function

This function randomly shuffles the characters in the string.

Syntax: str_shuffle(string);

string – the input string

Example input:

echo str_shuffle("Hello");
echo "
";
echo str_shuffle("Hello");

Example output:

lHole
oHell

The ord() function

The ord() function returns the ASCII value of the first character of the string.

Syntax: ord(string);

string – the input string

Example input:

echo ord("Hello");

Example output:

72

The str_repeat() function

This function is used to repeat printing a string a certain number of times.

Syntax: str_repeat(string,count);

string – the input string
count – the number of times the input string needs to be printed.
Example input:
echo str_repeat("*",10);
Example output:
**********
PS: This function could be used to create interesting patterns.

Monday, September 29, 2008

The strcmp() function

The strcmp() function compares two strings and returns an integer accordingly.

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

The implode function is used to glue together elements of an array into a string.

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

The explode() function is used to split a string into an array.

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

This function could be used to replace all or a portion of a string with another.

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.
Example input:
$str1="His name is James";
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);
Example output:
His name is James
Her name is James
Her name is Marie
Marie

The substr_count() function

This function could be used to count the number of times a sub-string occurs within a given string.

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.
Example input:
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr_count($str1,"PHP",3,20);
echo "
";
echo substr_count($str1,"PHP",3,10);
echo "
";
echo substr_count($str1,"i");
Example output:
This is a simple PHP program 1 0 3

The strtok() function

This function could be used to split a string into smaller strings.

Syntax: strtok(string,split);

split – the split character
Example input:
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo $str2 = strtok($str1," ");
echo "
";
echo strtok(" ");
echo "
";
echo strtok(" ");
Example output:
This is a simple PHP program This is a
A good look at the example and you will realize that the string is specified only for the first time strtok() is called. For subsequent calls, the string need not be mentioned as the function keeps track of the current string.

Friday, September 19, 2008

The substr() function

This function could be used to extract a certain part of the string.

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

This function is used to identify the position of a string within a larger string.
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

This function wraps a string into a new line when it reaches a specified length.

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

This function returns a string with backslashes added before certain characters.

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

This function returns a given string in upper case.

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

This function split a string into smaller strings.

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.
Example inputs:
$str1="A simple PHP Program";
echo chunk_split($str1,2,"@");

Sunday, September 14, 2008

The chr() function

chr() returns the character corresponding to an ASCII value

Example inputs:

echo chr(100);
echo "
";
echo chr(101);
echo "
";
echo chr(0101);
echo "
";
echo chr(0XFF);

The strlen() function

The strlen() function returns the length of string.

Syntax: strlen(string);

Example inputs:

$str1= "A simple PHP program";
echo strlen($str1);

Saturday, September 13, 2008

The strrev() function

This function is used to reverse any given string.
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

This has the same syntax and performs the same function rtrim() does.

The ltrim() function

ltrim() removes whitespace and certain pre-defined characters from the left end of the string.
Example inputs:
$str1=" A simple PHP Program";
echo $str1;
echo "
";
echo ltrim($str1);

trim() function

The trim() function could be used to remove certain pre-defined characters from both ends of a string.
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.
Example inputs
1.
$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

In PHP, strings could be easily concatenated using the dot (.) operator.

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

We know that to print individual elements of an array, the echo() of the print() function could be used.
However, to print an array as such, we must use the print_r() function.
Syntax: print_r($expression, $return);

$expression the expression to be printed.
$return
if this parameter is set to TRUE, the print_r() function would return its output to a variable.
if this parameter is FALSE, the output would be printed. FALSE is the default value.

Also, remember that the print_r() function is not used only for printing arrays, but also for any mixed expressions.
Example input:
1.
$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;
Example output:
1.
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??
2.
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 ) )

Wednesday, September 10, 2008

Data types in PHP

In PHP the data type of a variable is not set by the programmer. PHP decides the data type of variables after interpreting the web page. Data Types in PHP include:
Numeric data type
String data type
Array data type
Numeric Data types
1. Integer Data Type
Integer data types contain whole number values. Integer values range from -32767 to 32768.
2. Doubles
Double contains floating point numbers.
String Data types
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.
Arrays can be classified into:
Numeric arrays
Associative arrays
Multi-dimensional arrays
Numeric 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.
There are two ways of creating an associative array as shown in the example.Multi-dimensional Arrays.
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.
EXAMPLES:
1.
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?;
You could access elements of associative arrays as:
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

The print function is used to print one or more variable in the output.
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 does an echo function do?
What is the difference between using single quotes and double quotes in the echo function?

Friday, September 5, 2008

The echo function

The echo function is used to print one or more variable in the output.
Syntax: echo variable-name;
Example inputs
1.
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’;
Example outputs:
1.
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 is the assignment operator in PHP?
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

How does a block of PHP code begin and how does it end?
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

Who invented PHP?
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

PHP is the successor of PHP/FI that was developed by Rasmus Lerdorf. PHP/FI, or Personal Home Page / Forms Interpreter, had a simple set of Perl scripts and was used by Mr. Lerdorf for tracking accesses to his online resume.

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 does PHP stand for?
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?

Thursday, July 31, 2008

What is PHP?

PHP (Hypertext Preprocessor) is a server-side scripting language meant to create dynamic web pages and is run on web servers like Apache. PHP takes the code/script you write as input and provides a web page as its output.

It is an open-source programming language and could be deployed on most web servers, operating systems and platforms free of charge.

PHP also supports most databases like MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.
As mentioned earlier, PHP is a server-side scripting language. This means that a PHP page is processed on a Web server by a PHP script engine. When the client makes a request for any page, the request is sent to the server; the server locates the requested page, executes the PHP code and sends the result to the browser (client) in the form of HTML. The browser then compiles the HTML to display the output.

Now, you’d be probably thinking, “why PHP? Why not the other web-programming languages?”

Firstly, PHP is cross-platform.

Furthermore, PHP is a regular and a more well-defined language. It is hence easier to learn than most other programming languages such as Java.

And, due to this very reason, PHP is becoming increasingly used everyday by programmers all over the world for building web applications.

Java is one of the many programming languages that are used for developing web-applications. Despite being object-oriented, Java comes with its own drawbacks. One drawback is its complexity – the complexity presented by the several layers it contains. And applications developed using PHP are several times faster than those developed with Java.

As PHP evolved, it brought into it the best features of Java, Python, PERL, C, C++ etc.