Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Monday, April 27, 2009

Some PHP Exercises

Write a PHP program to set your locale information to that of theUnited Kingdom and display the currency of the UK. Once you are done, set the locale information to default.

Sunday, April 26, 2009

The localeconvo() function

This function returns an array that contains local numeric and currency formatting information.

syntax: localconvo();


The array elements in the array returned by this function and their meanings are listed below:

[decimal_point] - Decimal point character

[thousands_sep] - Thousands separator

[int_curr_symbol] - Currency symbol (example: USD)

[currency_symbol] - Currency symbol (example: $)

[mon_decimal_point] - Monetary decimal point character

[mon_thousands_sep] - Monetary thousands separator

[positive_sign] - Positive value character

[negative_sign] - Negative value character

[int_frac_digits] - International fractional digits

[frac_digits] - Local fractional digits

[p_cs_precedes] - True (1) if currency symbol is placed in front of a positive value, False (0) if it is placed behind

[p_sep_by_space] - True (1) if there is a spaces between the currency symbol and a positive value, False (0) otherwise

[n_cs_precedes] - True (1) if currency symbol is placed in front of a negative value, False (0) if it is placed behind

[n_sep_by_space] - True (1) if there is a spaces between the currency symbol and a negative value, False (0) otherwise

[p_sign_posn] - Formatting options:
0 - Parentheses surround the quantity and currency symbol
1 - The + sign is placed in front of the quantity and currency symbol
2 - The + sign is placed after the quantity and currency symbol
3 - The + sign is placed immediately in front of the currency symbol
4 - The + sign is placed immediately after the currency symbol

[n_sign_posn] - Formatting options:
0 - Parentheses surround the quantity and currency symbol
1 - The - sign is placed in front of the quantity and currency symbol
2 - The - sign is placed after the quantity and currency symbol
3 - The - sign is placed immediately in front of the currency symbol
4 - The - sign is placed immediately after the currency symbol

[grouping] - Array displaying how numbers are grouped (example: 3 indicates 1 000 000)

[mon_grouping] - Array displaying how monetary numbers are grouped (example: 2 indicates 1
00 00 00)

Example input:

echo setlocale(LC_ALL,NULL);
print_r(localeconv());

Example output:

English_United States.1252Array ( [decimal_point] => . [thousands_sep] => , [int_curr_symbol] => USD [currency_symbol] => $ [mon_decimal_point] => . [mon_thousands_sep] => , [positive_sign] => [negative_sign] => - [int_frac_digits] => 2 [frac_digits] => 2 [p_cs_precedes] => 1 [p_sep_by_space] => 0 [n_cs_precedes] => 1 [n_sep_by_space] => 0 [p_sign_posn] => 3 [n_sign_posn] => 0 [grouping] => Array ( [0] => 3 ) [mon_grouping] => Array ( [0] => 3 ) )

Friday, April 24, 2009

Some PHP Exercises

What is the use of the setlocale() function?

Write a program to set your locale information to the UK and then revert back to your default locale information.

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 )

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 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?

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?