Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

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

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);

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

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

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.