Learn Java and PHP Part 1

Introduction to Java and PHP

Java and PHP are popular programming languages and are widely used for web applications. A Java application can be compiled to byte-code as a class file which can be executed on any Java Virtual Machine (JVM).

PHP is PHP Hypertext Preprocessor. PHP is a scripting language with syntax similar to C, Java and Perl. Both PHP and Java are server-side languages which can be used to build stand-alone applications

In this tutorial, Java and PHP will be installed and the hello world program will be compiled and executed.

    Tools are required:

  • Text editor for creating and modifying the Java and PHP files.
  • Java.
  • PHP.

Download and install Java JDK and JRE

JDK stands for Java Development Kit, and JRE stands for Java Run-time Environment which is the virtual machine.

    Steps to install JDK and JRE:

  1. Point your browser to http://openjdk.java.net.
  2. Download the latest JDK and follow the link to download the JRE.
  3. Click the install package.
  4. Agree to the license in order to continue the installation.
  5. Alternatively, some operating systems such as Linux have JDK and JRE in their repositories.

Download and install PHP

PHP comes as a command-line tool which modules to extend its functions.

    Steps to install PHP:

  1. Point your browser to http://php.net.
  2. Download the latest PHP or follow the link to download the PHP binary files.
  3. Click the install package.
  4. Agree to the license in order to continue the installation.
  5. Alternatively, some operating systems such as Linux have PHP in their repositories.

Download and install suitable text editor

Any text editor will work. A bare minimum should be syntax-highlighting for Java and PHP. To select a reviewed lightweight programming editor read the Ojambo.com Lightweight Programming Editors.

    Steps to install Geany:

  1. Point your broswer to http://geany.org.
  2. Download the latest Geany or follow the link to download the Geany binary files.
  3. Click the install package.
  4. Agree to the license in order to continue the installation.
  5. Alternatively, some operating systems such as Linux have Geany in their repositories.

Lesson 0: Syntax

Java PHP
public static void main(String[] args) { } <?php ?>

Java requires a main method which contains a command line argument. In the example, values of a String type are being passed as an array. The variable name is args. PHP requires everything to be contained in tags ending with ?>.

Lesson 1: Comments

Java PHP
// This is end-of-line comment for Java // This is end-of-line comment for PHP
/* This is multi-line
comment for Java
*/
/* This is multi-line
comment for PHP
*/

Both Java and PHP use the same types of comments for both end-of-line and multi-line.

Lesson 2: Classes

Java PHP
public class Foo {
public void wow() { }
}
class Foo {
function wow() { }
}
Foo foo = new Foo();
Foo.wow();
$foo = new Foo();
$foo.wow();

For both Java and PHP, the object specification is class. The blocks are defined by Curly brackets or Squiggly Brackets { }. In Java, the class name’s first letter must be a capital. Initialization for Java and PHP is similar expect that PHP requires a dollar sign $ when instantiating classes. Java calls objects inside classes methods while PHP refers to them as functions.

Lesson 3: Hello World Program

Java PHP
System.out.println(“Hello World!”); echo “Hello World!”

Displaying output differs in both Java and PHP. Java uses println while PHP uses echo to display one or more strings. PHP can also use print or printf to display one string.

Lesson 4: Compile and Execute

Java PHP
javac foo.java php -l foo.php
java foo php foo.php

For Java, javac compiles classes and creates a class file such as foo.class. PHP with the option “-l” compiles and outputs any errors. In Java, when java is executed, it invokes the static main method.
PHP just executes the code without any options.

HelloWorld.java file

/**
 * Ojambo.com Hello World Java Tutorial
 * HelloWorld.java Copyright 2011 Edward
 * http://ojambo.com
 * 
 */
 
/* This is an example of
	Multi-line comments for java
*/

// This is an example of end-of-line comment for Java

// This class displays Hello World
public class HelloWorld { //This class is called HelloWorld
    public static void main(String[] args) { // Main Method
        System.out.println("Hello World!"); // Print the string Hello World!.
    }
}

The HelloWorld.java file can be compiled as javac HelloWorld.java and executed with java HelloWorld.

HelloWorld.php file

<?php
/*
 * Ojambo.com Hello World PHP Tutorial
 * HelloWorld.php Copyright 2011 Edward
 * http://ojambo.com
 * 
 */
 
/* This is an example of
	Multi-line comments for PHP
*/

// This is an example of end-of-line comment for PHP

// This displays Hello World
echo "Hello World!";

?>

The HelloWorld.php can be compiled as php -l HelloWorld.php and compiled with php HelloWorld.php

Conclusion:

There are notable differences between Java and PHP. Java is case sensitive and classes must begin with a capital letter. In Java, every method which are known as functions in PHP must be part of a class. In order for Java to compile comments, the commenting style must be opened with a slash and two asterisks /**. There are many similarities between Java and PHP such as statements, the use of semicolons, and curly or brace brackets for blocks.

    Recommendations:

  1. This tutorial uses an optional lightweight programming editor with syntax-highlighting features.
  2. The concepts taught in the first four lessons can be applied to other programming languages.
  3. When selecting a programming language to master, pay close attention to license restrictions.

Tags: , , , , , , , , , , ,

This entry was posted on Wednesday, June 1st, 2011 at 12:00 am and is filed under Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 responses to “Learn Java and PHP Part 1”

  1. […] Introduction to Java and PHP Part 1. […]

  2. […] Introduction to Java and PHP Part 1. […]

  3. […] Video for the Ojambo.com Introduction to Java and PHP Part 1. Part One of […]

  4. […] Introduction to Java and PHP Part 1. […]

Leave a Reply

Your email address will not be published. Required fields are marked *