Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9279961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:45:23+00:00 2026-06-18T17:45:23+00:00

I’m a bit stuck and I hope someone who’s more familiar with PHP will

  • 0

I’m a bit stuck and I hope someone who’s more familiar with PHP will be able to help me. Basically I am writing a PHP application that lets you plug in products via an XML file. When I say products I mean like a car-hire or insurance type product, where there are forms to fill out and rates to calculate. I opted for an XML structure to make it easy in future to add new or adapt them without any coding knowledge as the underlying class will do all the work.
However I’m now trying to rack my brain for the safest and best way to include rates calculations in the XML file structure and evaluate them in PHP, without any risk of being exploited.
So for example the xml file structure I will have for the rates calculation area might look something like

<rates>
  <rate name="variable1" type="forminput" relative="form['input1']"></rate>
  <rate name="variable2" type="forminput" relative="form['input2']"></rate>
  <rate name="calculatedvariable1" type="calculation">[variable1] * [variable2]</rate>
</rates>

Ok so this is a pretty basic example but the idea is that my class then reads the name, creates a php variable based on this using an xml parser, with the value given in the form (or wherever the relative is referencing) and then when it finds a calculation type it plugs the correctly named variables (noted by square brackets or whatever) into the calculation and evaluates it.
So in this example the output given by parsing the calculation will be :

$variable1 * $variable2

Which then needs to run as a calculation to get the value of calculatedvariable1. Now I know I can use eval() to achieve this but I’m not convinced this is the only option and perhaps I’m missing something else I could be doing either in my xml structure or in the way my php will parse it.
Anyhow I hope I’ve explained it properly and thanks in advance for any ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:45:24+00:00Added an answer on June 18, 2026 at 5:45 pm

    Considering you have solved the problem to retrieve the value from the form already in form of a string based on the relative attribute – naming the implementation here resolve_variable($relative).

    You can then assign the value to the element itself and formulate your calculation in form of a standard xpath 1.0 expression like:

    rate[@name="variable1"] * rate[@name="variable2"]
    

    And that expression inside the XML:

    <rates>
      <rate name="variable1" type="forminput" relative="form['input1']"></rate>
      <rate name="variable2" type="forminput" relative="form['input2']"></rate>
      <rate name="calculatedvariable1" type="calculation">
        rate[@name="variable1"] * rate[@name="variable2"]
      </rate>
    </rates>
    

    All you need in PHP is to make use of the DOMXPath object for your DOMDocument:

    $dom = new DOMDocument();
    $dom->loadXML($xml); 
    $xpath = new DOMXPath($dom);
    

    You can then assign the values to the nodes:

    // assign values
    foreach ($xpath->query('rate/@relative') as $relative) {
        $relative->ownerElement->nodeValue = resolve_variable($relative);
    }
    

    Then perform all the calculation(s):

    // do calculations
    $results = array();
    foreach ($xpath->query('rate[@type="calculation"]') as $calculation) {
        $results[$calculation->getAttribute('name')]
            = $xpath->evaluate($calculation->nodeValue);
    }
    

    They then come out nicely in form of the $results array (in my example I have 3 and 5 as values):

    Array
    (
        [calculatedvariable1] => 15
    )
    

    Maybe this is helpful for you.

    Take care that if you need precise calculations floating point arithmetics might stand in your way http://www.validlab.com/goldberg/paper.pdf

    A working example at a glance for the whole picture / the view from above: http://eval.in/9595

    You might be able to do this as well with XSL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.