• Around The HomeToggle Menu

    • Entertainment
    • Productivity
    • Smart Home
  • FamilyToggle Menu

    • Parenting
    • Toys
    • Pets
    • Travel
  • Product ReviewsToggle Menu

    • Phones
    • Tablets
    • Laptops
    • Desktops
    • Wearables
    • Audio
    • Cameras
    • Headphones
    • Printers
    • Smart Home
    • TVs
    • Gaming and Video
  • One Cool ThingToggle Menu

    • Frugal Tech
    • Kickstarters
    • Videos
Techwalla
  1. Home
  2. Around The Home
  3. Productivity
  4. How to Convert From Java to PHP

How to Convert From Java to PHP

March 31, 2015
By: Joey Liam
  • Share
  • Share on Facebook

Java and PHP are two of the most used programming languages today. Although both technologies offer advanced functionality and dynamic portability, converting one to the other is not quite possible yet. The good news though is that Java and PHP classes can be integrated together to form efficient applications, such as for sales tax calculation. The main script to be used for this PHP-Java integration project is PHP script and the compiler will be Java.

Video of the Day

...
Java integrates well with PHP.

Step

Paste the code below to establish your Java compiler, which you will need to compile your salesTax.java project within the PHP script:

import java.util.; import java.text.;

public class SalesTax {

public String SalesTax(double price, double salesTax) {

numberFormatter.format(salesTax);

}

Step

Run PHP to call the compiled code called SalesTaxInterface.php. This function will calculate the data inputted by the user in your HTML form:

<?php

// Format the HTML form.

$salesTaxForm = <<

Price (ex. 42.56):


Sales Tax rate (ex. 0.06):


SalesTaxForm;

if (! isset($submit)) :

echo $salesTaxForm;

else :

// Instantiate the SalesTax class. $salesTax = new Java("SalesTax");

// Don't forget to typecast in order to

// conform with the Java method specifications.

$price = (double) $price; $tax = (double) $tax;

print $salesTax->SalesTax($price, $tax);

endif;

?>

Step

Add the following code to your project to bridge Java and PHP together:

#!/bin/env php <?php

require_once("java/Java.inc");

include("wsimport.php");

try { $addNumbersService = new java("org.duke.AddNumbersService"); $port = $addNumbersService->getAddNumbersPort(); $number1 = 10; $number2 = 20; echo ("Invoking one-way operation. Nothing is returned from service.\n"); $port->oneWayInt($number1); echo ("Invoking addNumbers($number1, $number2)\n"); $result = $port->addNumbers($number1, $number2); echo ("The result of adding $number1 and $number2 is $result\n\n"); $number1 = -10; echo ("Invoking addNumbers($number1, $number2)\n"); $result = $port->addNumbers($number1, $number2); echo ("The result of adding $number1 and $number2 is $result\n\n"); } catch (JavaException $ex) { $ex = $ex->getCause(); if (java_instanceof($ex, java("org.duke.AddNumbersFault_Exception"))) { $info = $ex->getFaultInfo()->getFaultInfo (); echo ("Caught AddNumbersFault_Exception: $ex, INFO: $info.\n"); } else { echo ("Exception occured: $ex\n"); } } ?>

Show Comments

Related Articles

How to Put Pictures in Java BlueJ

How to Put Pictures in Java BlueJ

Around The Home
Productivity
By: Mark Stansberry
Advantages & Disadvantages of Java Virtual Machine Interpreter

Advantages & Disadvantages of Java Virtual Machine Interpreter

Around The Home
Productivity
By: demand86857
How to Read PDF File in Java

How to Read PDF File in Java

Around The Home
Productivity
By: James Cooper
How Do I Enable Java Applets in Firefox?

How Do I Enable Java Applets in Firefox?

Around The Home
Productivity
By: Shea Laverty
How to Insert the Date and Time in a Website

How to Insert the Date and Time in a Website

Around The Home
Productivity
By: demand86857
How to Create a Quiz With Random Questions Using Java

How to Create a Quiz With Random Questions Using Java

Around The Home
Productivity
By: Micah McDunnigan
  • HOW WE SCORE
  • ABOUT US
  • CONTACT US
  • TERMS
  • PRIVACY POLICY
  • COPYRIGHT POLICY
  • Advertise

An error occurred. Try again later.

Thanks for signing up!
© 2018 Leaf Group Ltd. Leaf Group Media

Get great tech advice delivered to your inbox.

Keep your family productive, connected, entertained, and safe.

Please enter a valid email.