MY mENU


Tuesday 13 March 2012

Use of PHP


Use of PHP:  There are three main fields you can use PHP:
  • Websites and web applications (server-side scripting)
  • Command line scripting
  • Desktop (GUI) applications
For the first and most common form, you need three things: PHP itself, a web server and a web browser. You probably already have a web browser, and depending on your operating system setup, you may also have a web server (e.g. Apache on Linux and MacOS X; IIS on Windows). You may also rent webspace at a company. This way, you don't need to set up anything on your own, only write your PHP scripts, upload it to the server you rent, and see the results in your browser.
In case of setting up the server and PHP on your own, you have two choices for the method of connecting PHP to the server. For many servers PHP has a direct module interface (also called SAPI). These servers include Apache, Microsoft Internet Information Server, Netscape and iPlanet servers. Many other servers have support for ISAPI, the Microsoft module interface (OmniHTTPd for example). If PHP has no module support for your web server, you can always use it as a CGI or FastCGI processor. This means you set up your server to use the CGI executable of PHP to process all PHP file requests on the server.
If you are also interested to use PHP for command line scripting (e.g. write scripts autogenerating some images for you offline, or processing text files depending on some arguments you pass to them), you always need the command line executable. In this case, you need no server and no browser.
With PHP you can also write desktop GUI applications using the PHP-GTK extension. This is a completely different approach than writing web pages, as you do not output any HTML, but manage windows and objects within them.PHP-GTK is not included in the official PHP distribution.

Versions of PHP


PHP/FI 1 
when Rasmus Lerdorf first started developing PHP/FI.The first version of "PHP/FI," called Personal Homepage Tools/Form Interpreter, was a collection of Perl scripts in 1995. One of the basic features was a Perl-like language for handling form submissions, but it lacked many common useful language features, such as for loops.
PHP/FI 2
A rewrite came with PHP/FI 2 in 1997, but at that time the development was almost solely handled by Rasmus. After its release in November of that year, Andi Gutmans and Zeev Suraski bumped into PHP/FI while looking for a language to develop an e-commerce solution as a university project. They discovered that PHP/FI was not quite as powerful as it seemed, and its language was lacking many common features. One of the most interesting aspects included the way while loops were implemented. The hand-crafted lexical scanner would go through the script and when it hit the while keyword it would remember its position in the file. At the end of the loop, the file pointer sought back to the saved position, and the whole loop was reread and re-executed.
PHP 3
Zeev and Andi decided to completely rewrite the scripting language. Then they teamed up with Rasmus to release PHP 3, and along also came a new name: PHP: Hypertext Preprocessor, to emphasize that PHP was a different product and not only suitable for personal use. Zeev and Andi had also designed and implemented a new extension API. This new API made it possible to easily support additional extensions for performing tasks such as accessing databases, spell checkers and other technologies, which attracted many developers who were not part of the "core" group to join and contribute to the PHP project. PHP 3’s release in June 1998.
PHP 4
In late 1998, Zeev and Andi looked back at their work in PHP 3 and felt they could have written the scripting language even better, so they started yet another rewrite. While PHP 3 still continuously parsed the scripts while executing them, PHP 4 came with a new paradigm of "compile first, execute later." The compilation step does not compile PHP scripts into machine code; it instead compiles them into byte code, which is then executed by the Zend Engine (Zend stands for Zeev & Andi), the new heart of PHP 4. Because of this new way of executing scripts, the performance of PHP 4 was much better than that of PHP 3, with only a small amount of backward compatibility breakage.
Among other improvements was an improved extension API for better run-time performance, a web server abstraction layer allowing PHP 4 to run on most popular web servers, and lots more. PHP 4 was officially released on May 22, 2002. In PHP 3, the minor version number (the middle digit) was never used, and all versions were numbered as 3.0.x. This changed in PHP 4, and the minor version number was used to denote important changes in the language. The first important change came in PHP 4.1.0, which introduced superglobals such as $_GET and $_POST .
 Superglobals can be accessed from within functions without  having to use the global keyword. This feature was added in order to allow the register_globals INI option to be turned off. register_globals is a feature in PHP which automatically converts input variables like "?foo=bar" in http://php.net/?foo=bar to a PHP variable called $foo. Because many people do not check input variables properly, many applications had security holes, which made it quite easy to circumvent security and  authentication code.
With the new superglobals in place, on April 22, 2002, PHP 4.2.0 was released with the register_globals turned off by default. PHP 4.3.0, the last significant PHP 4 version, was released on December 27, 2002. This version introduced the Command Line Interface (CLI), a revamped file and network I/O layer (called streams), and a bundled GD library. Although most of those additions have no real effect on end users, the major version was bumped due to the major changes in PHP’s core.
PHP 5
Soon after, the demand for more common object-oriented features increased immensely, and Andi came up with the idea of rewriting the objected-oriented part of the Zend Engine. Zeev and Andi wrote the "Zend Engine II: Feature Overview and Design" document and jumpstarted heated discussions about PHP’s future. Although the basic language has stayed the same, many features were added, dropped, and changed by the time PHP 5 matured.
For example, namespaces and multiple inheritance, which were mentioned in the original document, never made it into PHP 5. Multiple inheritance was dropped in favor of interfaces, and namespaces were dropped completely. 
The rewritten XML functionality in  PHP 5 puts it on par with other web technologies in some areas and overtakes them in others, especially due to the new SimpleXML extension which makes it ridiculously easy to manipulate XML documents. In addition, the new SOAP, MySQLi, and variety of other extensions are significant milestones in PHP’s support for additional technologies.

History of PHP


Some time in 1994 when Rasmus Lerdorf put together a bunch of Perl scripts to track down who was looking at his resume. Little by little, people started to get interested in the scripts, and they were later released as a package "Personal Home Page" tools (the first meaning of PHP). In view of the interest, he wrote a scripting engine and incorporated another tool to parse input from HTML forms: FI, Form Interpreter, thus creating what was called variously PHP/FI or PHP2. This was done around mid 1995. 
Soon, people started to use these tools to do more complicated things, and the development changed from just one person, to a group of core developers in charge of the project and its organization. This was the beginning of PHP3. This group of developers (Rasmus Lerdorf, Andi Gutmans, Zeev Suraski, Stig Bakken, Shane Caraveo, and Jim Winstead), improved and extended the scripting engine and added a simple API that will allow other programmers the liberty to add more functionality to the language by writing modules for it.The language's syntax was also refined, with constructs that will be familiar for people coming from object oriented or procedural languages. If you know C, C++ or Java, or have done even some shell/awk scripting, or written a Pascal or VBasic program, learning the basic PHP constructs will be a breeze.

Threads points



Some Important points about Threads:
  • A Java program runs until the only threads left running are daemon threads.
  • A Thread can be set as a user or daemon thread when it is created.
  • In a standalone program, your class runs until your main() method exists - unless your main() method creates more threads.
  • You can initiate your own thread of execution by creating a Thread object, invoking its start() method, and providing the behaviour that tells the thread what to do. The thread will run until its run() method exists, after which it will come to a halt - thus ending its life cycle.
  • The Thread class, by default, doesn’t provide any behaviour for run().
  • There are two ways to provide the behaviour for a thread
  • Subclass the thread and override the run() method - see p.253 of Exam Guide.
  • Implement the Runnable interface and indicate an instance of this class will be the thread’s target.
  • A thread has a life cycle. Creating a new Thread instance puts the thread into the “new thread” state. When the start() method is invoked, the thread is then “alive” and “runnable”. A thread at this point will repond to the method isAlive() by returning true.
  • The thread will continue to return true to isAlive() until it is “dead”, no matter whether it is “runnable” or “not runnable”.
  • If 2 threads are alive, with the same highest priority, the JVM switches between them. The JVM will switch between any number of threads with the same highest priority.
  • The priority numbers for threads falls between the range of Thread.MIN_PRIORITY and Thread.MAX_PRIORITY.
  • The default thread priority is Thread.NORM_PRIORITY.
  • New threads take on the priority of the thread that spawned them.
  • You can explicitly set the priority of a thread using setPriority(), and you can get the priority of a thread using getPriority(). There is no constructor for thread which takes a priority.
  • The JVM determines the priority of when a thread can run based on its priority ranking, but this doesn’t mean that a low priority thread will not run.
  • The currently executing thread can yield control by invoking yield(). If you invoke yield(), Java will pick a new thread to run. However, it is possible the thread that just yielded might run again immediately if it is the highest priority thread.
  • There are 3 types of code that can be synchronized: class methods, instance methods, any block of code within a method.
  • Variables cannot take the synchronized keyword.
  • Synchronization stays in effect if you enter a synchronized method and call out to a non-synchronized method. The thread only gives up the monitor after the synchronized method returns.
  • Using a thread’s stop() method will make it die.
  • There are 3 ways to transition a thread between “runnable” and “not runnable”
  1. put it to sleep and wake it up (not on the test)
  2. pause it and resume it (not on the test)
  3. use the methods wait(), notify() and notifyAll() - these are methods of class Object. wait() throws InterruptedException.
  • The third method listed above allows communication between the threads, whereas the other 2 methods don’t.
  • By using the methods wait(), notify() and notifyAll(), any thread can wait for some condition in an object to change, and any thread can notify all threads waiting on that object’s condition that the condition has changed and that they should continue.
  • When a waiting thread pauses, it relinquishes the object’s monitor and waits to be notified that it should try to reacquire it.
  • If you know you only have one thread waiting on a condition, you can feel free to use notify(), otherwise you should use notifyAll(). The notifyAll() method wakes up all threads waiting to reacquire the monitor for the object.
  • The reasons why a thread might be “alive” and “not runnable” are as follows:
  • The thread is not the highest priority thread and so is not able to get CPU time
  • The thread has been put to sleep using the sleep() method (of class Thread. Throws InterruptedException)
  • The thread has been suspended using the suspend() method
  • The thread is waiting on a condition because someone invoked wait() for the thread
  • The thread has explicitly yielded control by invoking yield()

TabHost in Android


Some times we want to wrap multiple views in a single window and navigate throught them with a Tab Container. this can be done in Android using TabHost control. There are two ways to use a TabHost application in Android:
1.     Using the TabHost to navigate through multiple views within the same activity.
2.     Using the TabHost to navigate through Actual multiple Activities using intents.
Anatomy of Tabbed Application An activity with a TabHost may look like this:

The Activity consists of:
1.     A TabHost: the root element of the layout.
2.     The TabHost wraps a TabWidget which represents the tab bar.
3.     The TabHost wraps a FrameLayout which wraps the contents of each tab.
There are some rules that we must stick to when using a Tabbed activity:
1.     If the activity is of type TabActivity [optional] then the TabHost must have the id @android:id/tabhost.
2.     The TabWidget must have the id @android:id/tabs.
3.     The FrameLayout must have the id @android:id/tabcontent.
Now let's see an exaple to an activity with multiple tabs:  this is the layout file


<?xml version="1.0" encoding="utf-8"?>

    <TabHost android:layout_width="fill_parent"

    android:layout_height="fill_parent"


    android:id="@+id/tabHost"



>

    <TabWidget


    android:layout_width="fill_parent"

    android:layout_height="wrap_content"


    android:id="@android:id/tabs"

    />


     <FrameLayout

     android:layout_width="fill_parent"


    android:layout_height="fill_parent"

    android:id="@android:id/tabcontent"


     >

     <LinearLayout


     android:layout_width="fill_parent"

    android:layout_height="wrap_content"


    android:id="@+id/tab1"

    android:orientation="vertical"


    android:paddingTop="60px"

     >


     <TextView 

    android:layout_width="fill_parent"


    android:layout_height="100px"

    android:text="This is tab1"


    android:id="@+id/txt1"

    />


     


     </LinearLayout>

      


     <LinearLayout

     android:layout_width="fill_parent"


    android:layout_height="fill_parent"

    android:id="@+id/tab2"


    android:orientation="vertical"

    android:paddingTop="60px"


     >

     <TextView 


    android:layout_width="fill_parent"

    android:layout_height="100px"


  android:text="This is tab 2"

    android:id="@+id/txt2"


 />

    


     </LinearLayout>

      


      <LinearLayout

     android:layout_width="fill_parent"


    android:layout_height="fill_parent"

    android:id="@+id/tab3"


    android:orientation="vertical"

    android:paddingTop="60px"


     >

     <TextView 


    android:layout_width="fill_parent"

    android:layout_height="100px"


    android:text="This is tab 3"

    android:id="@+id/txt3"


    />

    


     </LinearLayout>

     </FrameLayout>


    </TabHost>
then in the code of our activity class

public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);


        setContentView(R.layout.main);

        TabHost tabHost=(TabHost)findViewById(R.id.tabHost);


        tabHost.setup();

         


        TabSpec spec1=tabHost.newTabSpec("Tab 1");

        spec1.setContent(R.id.tab1);


     spec1.setIndicator("Tab 1");

     


        TabSpec spec2=tabHost.newTabSpec("Tab 2");

        spec2.setIndicator("Tab 2");


        spec2.setContent(R.id.tab2);

         


        TabSpec spec3=tabHost.newTabSpec("Tab 3");

        spec3.setIndicator("Tab 3");


        spec3.setContent(R.id.tab3);

         


        tabHost.addTab(spec1);

        tabHost.addTab(spec2);


        tabHost.addTab(spec3);

         


 }
Is going to look like this:

1.     We create tabs using TabSpecs class.
2.     We set the title of each tab using TabSpecs.setIndicator() method.
3.     We set the content of each tab using TabSpecs.setContent() method.
4.     if you use TabActivity to as a base class to your activity, you do not need to call TabHost.Setup() method.
We can add an icon to the title of the tab like this:

TabSpec spec1=tabHost.newTabSpec("Tab 1");

        spec1.setContent(R.id.tab1);


spec1.setIndicator("Tab1",getResources().getDrawable(R.drawable.flash));

         


        TabSpec spec2=tabHost.newTabSpec("Tab 2");


spec2.setIndicator("Tab2",getResources().getDrawable(R.drawable.sun));

        spec2.setContent(R.id.tab2);


        TabSpec spec3=tabHost.newTabSpec("Tab 3");


    spec3.setIndicator("Tab3",getResources().getDrawable(R.drawable.chart));

        spec3.setContent(R.id.tab3);
It will look like this:
we can also specify the indicator to be a view:

TabSpec spec1=tabHost.newTabSpec("Tab 1");

        spec1.setContent(R.id.tab1);


        TextView txt=new TextView(this);

        txt.setText("Tab 1");


        txt.setBackgroundColor(Color.RED);

      spec1.setIndicator(txt);
Setting the content of tabs: we saw how to set the contents of tabs by specifying multiple layout resources to be displayed within the same activity.

what If we have multiple Activities in our application and we want to navigate between them using tabs ?
in this case we will have one activity as the root activity of the application. this activity will have the TabHost and will navigate to other activities using Intents.
Note: the root activity must inherit from TabActivity.

the root activity will have layout file like this:

<?xml version="1.0" encoding="utf-8"?>

    <TabHost android:layout_width="fill_parent"


    android:layout_height="fill_parent"

    android:id="@android:id/tabhost"



    >


    <TabWidget

    android:layout_width="fill_parent"


    android:layout_height="wrap_content"

    android:id="@android:id/tabs"


    />

     <FrameLayout


     android:layout_width="fill_parent"

    android:layout_height="fill_parent"


    android:id="@android:id/tabcontent"

     >


     </FrameLayout>

    </TabHost>

the other activities will have a simple layout consisting of a TextView. Now to the code of the root activity

public class TabDemo extends TabActivity {

    /** Called when the activity is first created. */


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        TabHost tabHost=getTabHost();


        // no need to call TabHost.Setup()      



        //First Tab

        TabSpec spec1=tabHost.newTabSpec("Tab 1");

spec1.setIndicator("Tab1",getResources().getDrawable(R.drawable.sun));

   Intent in1=new Intent(this, Act1.class);


        spec1.setContent(in1);
         

TabSpec spec2=tabHost.newTabSpec("Tab 2");

     spec2.setIndicator("Tab2",getResources().getDrawable(R.drawable.chart));

        Intent in2=new Intent(this,Act2.class);

        spec2.setContent(in2);


        tabHost.addTab(spec2);

        tabHost.addTab(spec3);

    }

}
and the activity will look like this

Adding tabs at run-time:  
we can add tabs to TabHost at run-time using TabSpec.setContent(TabContentFactory) method. 
The TabContentFactory is an interface that requires the implementation of a callback method createTabContent(String tag) which returns the view to be added to the content of the tab. So in the last example if we changed code that adds the content of the second tab to this:



TabSpec spec1=tabHost.newTabSpec("Tab 1");

spec1.setIndicator("Tab1",getResources().getDrawable(R.drawable.sun));


        spec1.setContent(new TabContentFactory() {

   @Override

 public View createTabContent(String tag) {

    // TODO Auto-generated method stub


    return (new AnalogClock(TabDemo.this));


   }

 });

the activity will look like this