Archive

Archive for the ‘PHP Tutorials’ Category

PHP5 Tutorial – Magic Methods – __call() method

November 3rd, 2007 admin 2 comments

This tutorial will teach you how and when to use the magic method __call().

The magic method __call() is to undeclared methods what __get() and __set() are to undeclared data member.

These methods are automatically called internally when the program tires to execute a method that has not been defined within the class at the time of development.

Read more…

PHP5 Tutorial – Magic Methods – __isset() and __unset()

November 3rd, 2007 admin 6 comments

In my previous PHP5 OOPS tutorial on PHP5 Tutorial – Magic Methods – __get() and __set(), we learnt how and when to use these magic methods.

This PHP5 OOPS tutorial will teach you how and when to use the magic methods __isset() and __unset().

Read more…

PHP5 Tutorial on Arrays

October 31st, 2007 admin 7 comments

This PHP5 tutorial discusses about PHP arrays.

What is an array?
Generally speaking an array is continuous allocation of memory of similar data types.

This means that you can define a single array variable which can hold multiple values at the same time. These multiple values are retrieved using an index number. Don’t worry if you did not get this… more explanation ahead.




The same definition mentioned above is also applicable for PHP as well, with an exception that it can store data of different data types in one array.

Memory representation of an array

array.gif

In the above diagram, you can see that variable $a has been allocated 5 slots of memory in which data can be stored. Each of these slots / boxes are referred using an index number that starts from 0. A common mistake for people learning arrays is to consider the starting position of an array as 1.

Read more…

Categories: PHP, PHP Tutorials Tags:

PHP5 OOPS Tutorial __get() and __set()

October 26th, 2007 admin 2 comments

This article talks about the use of __get() (double underscore – get()) and __set() (double underscore – set()) PHP5 OOPS magic methods.




By default PHP is a Loosely typed language and therefore it is not necessary to declare variables before using them. This also holds true for using class members. Look at an example below.

Read more…

PHP5 Tutorial – Magic Methods – __toString() method

October 25th, 2007 admin 2 comments

PHP5 provides a magic method by the name of __toString() (double underscore followed by toString()) which is useful for debugging purposes.

The __toString() method is automatically called when an object in PHP5 is converted into a string for the purpose of display or concatenation.

Read more…

Tutorial 3 – PHP Strings

October 16th, 2007 admin 1 comment

In the last post I covered PHP variables and how to use them. In this tutorial we will understand how to use strings in PHP. If you are set to make a career in development then you would need to use a lot of strings to make your program / script work.

What is a String?

A string is nothing but a series of characters in a sequence. E.g: “Sunil”, “Programming”, “PHP”, “Tutorial” are all examples of Strings.

Creating a String in PHP

As discussed in PHP Tutorial 2 – Variables, PHP is a loosely typed language. Therefore you don’t have to declare a variable as String to use it (as in other programming languages like Java).

To declare a String you need to use double quotes (“) or single quotes (‘). Both these approaches have some differences that we will cover later. See the example below to understand how to create strings:

<?php
$string = “This is a string”;
$name = “Sunil Bhatia”;
$data = “My name is $name”;
echo $data;
?>

Read more…

Categories: PHP, PHP Tutorials Tags:

Tutorial 2 – PHP Variables

October 4th, 2007 admin 4 comments

In the earlier PHP tutorial we saw an introduction to what PHP is and how it is used in web programming. Click here to read that Post

In this tutorial on PHP Variables we will discuss the following:

  • What is a variable?
  • Variables in PHP
  • PHP is a loosely typed language
  • Variable naming rules in PHP
  • Use of the double dollar ($$) variables

What is a variable?

A variable is the name given to a memory allocation to store or retrieve information. A variable can be used to store values like strings (text), numbers, array or objects. Once a variable is set it can be used anywhere in your script.

Read more…

Categories: PHP, PHP Tutorials Tags:

How to configure PHP on Apache Windows

September 29th, 2007 admin No comments

If you have a stable installation of Apache, then installing PHP is very simple. Read the post on How to Install Apache on Windows if you wish to start with installing Apache first.

Lets begin the configuration.

Step 1: Download the Package

Visit http://www.php.net and download the latest stable release version of PHP which is PHP 5.2.4 as of this writing.

You should download the PHP 5.2.4 zip package.

Step 2: Extract the package

Extract the package to a folder on your drive. I work with C:\php5 and am going to use that as the PHP install folder for this tutorial.

Read more…

Categories: PHP Tutorials Tags:

Tutorial 1 – Introduction to PHP Web Programming

September 29th, 2007 admin 1 comment

Welcome to the first chapter of the tutorial on PHP Web Programming.

In this tutorial we will cover the following:

  • About PHP5 Scripting Language
  • PHP Tags
  • PHP Comments
  • How to display data on the browser
  • A note on line terminator
  • Installing PHP & Apache on your machine for development
  • Setting up Virtual Hosting on your machine for development

Fasten your seat belts and enjoy the lovely journey towards learning PHP 5.

About PHP5 Scripting Language

PHP5 is a scripting language for the web to render dynamic pages (See note below on dynamic and static pages). PHP is used for server side scripting and can also be used for developing CLI (Command Line Interface) programs. PHP is a recursive acronym for PHP: Hypertext Preprocessor.

Note on Dynamic and Static Pages

Initially when the World Wide Web was created all pages were static. This means that someone manually had to change the pages and upload it to the server again. This was a big issue in terms of creating a Web Application that would keep changing / updating a page depending on the data available in their database server.

Therefore, came the concept of CGI (Common Gateway Interface), through which you could ask for script pages instead of static HTML pages. Once a user hits the Script URL CGI would allow for the script engine to execute the page and throw HTML output to the browsers. This is called Dynamic Pages.

PHP Tags

With PHP it is possible to embed the presentation HTML along with your PHP Code. Therefore, for PHP to understand the area of your file where PHP code resides, you need to use PHP Tags.

Code Example:

<?php

echo “Hello World”;

? >
PHP allows 3 types of tags, as follows:

Standard Tags <?php
… code
?>
Short Tags <?=$variable?>
Script Tags <script language=”php”>
… code
</script>
ASP Tags <%
… code
%>

Standard tags are the de-facto opening and closing tags; they are the best solution for
portability and backwards compatibility, because they are guaranteed to be available
and cannot be disabled by changing PHP’s configuration file.

Short tags were, for a time, the standard in the PHP world; however, they do have
the major drawback of conflicting with XML headers and, therefore, have somewhat
fallen by the wayside. Their other advantage is the availability of the short form
<?=$variable ?> syntax, which allows you to print the result of an expression directly
to the script’s output.

Script tags were introduced so that HTML editors which were able to ignore
JavaScript but were unable to cope with the standard PHP tags could also ignore
the PHP code.

Nobody quite understands why ASP tags were introduced—however,
if you are so inclined you can turn on this optional configuration option, and you are
free to use them.

Note: Short tags, script tags and ASP tags are all considered deprecated and their use is
strongly discouraged.




PHP Comments

It is a good practice to comment your code. Comment is nothing but a textual explanation of what a piece of code does. This is necessary because over a period of time you as a developer would not remember how the logic works. Comments serve as inline documentation for the code and aids speedy maintenance activity.

PHP allows developers to comment as following:

Single line comment (//) <?php
//this logic does that
… code
?>
Single line comment (#) <?php
#this logic does that
… code
?>
Multi-line comment (/*…*/) <?php
/*this logic does that and requires
two lines of comments */
… code
?>

How to display data in PHP

PHP provides two methods of displaying data in the browser:

a) echo
b) print

Either of these statements can be used to display a string output on the browser or command line. Look at the examples below:

Example 1 : How to output a String
<?php

echo “Hello World”;
print “Hello World”;

?>

In the example above, look at the way Hello World has been enclosed in double quotes. Note that in PHP a string should be enclosed in double quotes. Though you could also enclose it with single quotes (‘Hello World’) because PHP is not type strict. But as a general practice it is better if you follow the double quote principle. By following this way of using string; if you keep developing projects across various different programming languages then the standard still gets maintained.




Example 2 : How to output a variable
<?php

$name = “Sunil Bhatia”;
echo “My name is ” . $name;
print “My name is ” . $name;

?>

In the example above we first assign a string to a variable $name (we will cover more about variables in the next tutorial). Look at how we have used the (.) dot operator to concatenate (join) two strings i.e. First String “My name is” and second string variable $name.

Concatenation means to join two strings so that the result of this operation becomes a joined string. The dot operator (.) is used to concatenate two strings. Just like we would use a + operator in Java or & operator in ASP.

A note on line terminators

As a developer you will be writing lines of code. But how would PHP know when a line of code is over. You would say that PHP should identify end of line because of the new line character i.e. <Enter> key. But there are many statements that you could be written on multiple lines or the same line. So, how would PHP identify end of line.

Like C, Java and C#; PHP also used a semi-colon (;) as a statement terminator. This is mandatory and should not be ignored.

Look at the example below

<?php

$name = “Sunil Bhatia”;
echo $name;

?>

Do you see that both the line of codes are terminated by a semi-colon (;).

Installing PHP & Apache on your machine for development

Installing PHP & Apache is easier than you think. Read this tutorial on the site to have a complete understanding:

How to Install Apache on Windows

The following link will help you understand how to configure PHP for Apache on Windows:

How to configure PHP on Apache for Windows

Setting up Virtual Hosting on your machine for development

Read this post on how to setup virtual hosting on your machine with Apache

Setting up Virtual Hosting in Apache on Windows

In the next tutorial we will learn about PHP Variables.

Cisco is considered one of the most important certification providers. Cisco exams CCNA IIUC Voice 640-460 and CCIE 350-029 are one of the most important certification exams. Cisco introduced 642-654 WAASSE Wide Area Application Services and 650-059 LCSARS Lifecycle Services Advanced Routing and Switching to enhance the network security. Microsoft MCSE 70-298 certification exam is the most popular exam.

Categories: PHP Tutorials Tags:

PHP5 Tutorials – Introduction

September 27th, 2007 admin No comments

Being a veteran Java programmer and a big believer of OOAD concepts I have started toying with PHP5 – which the world says to be the next level of PHP.

Guys behind PHP5 have no doubt done a remarkable job by releasing this version at a much needed time when the entire PHP community were wanting their favorite PHP language with OOPS features.

PHP5 has a couple of good OOPS features like:

  • You can now write Destructor methods
  • You can now define access specifiers (public, private & protected)
  • You can now inherit from a base class, therefore function overriding is now possible
  • You can now define Abstract Classes
  • You can now create Interfaces
  • PHP has now introduced Method Type Hinting

Compared to Java PHP5 does not have much to offer, but I sincerely believe that its a positive start towards making PHP5 a language as well defined by Java.

PHP6 is now under active development where a lot of new features are being thought of and support for certain global variables are removed.

Following is the course content that I will publish regularly

1. Introduction to PHP Web Programming
2. PHP variables
3. PHP Decision making Structures
4. PHP Looping Structures
5. PHP Functions
6. PHP Form Processing
7. PHP Cookie Management
8. PHP Database Management
9. PHP Sample Web Application
10. PHP Introduction to OOPS Features
11. PHP Classes, Objects and Methods
12. PHP Inheritance
13. PHP Interfaces
14. PHP Polymorphism
15. PHP Magic Methods
16. About Zend PHP Certification

Keep visiting back to find more updates on this tutorial

Note
PHP team has announced the end of PHP 4 by the 31st of December 2007. Beyond this date, no more fixes and patches for PHP4 will be available.

Get our latest and emerging certification related to HP0-A01 HP-UX 11i v3 System Administration, Microsoft MCTS 70-236 and JN0-400 JNCIA-EX. We offer free pdf download of Cisco 642-105 IUM Implementing Cisco Unified Messaging and Cisco 642-533 IPS. Get money back guarantee in the case of failure in the IBM 000-100 certification exam in your first attempt.

Categories: PHP Tutorials Tags: