3

i just need to add in a variable into the xpath my code is as followed

$target = $xml->xpath('//gig[@id=$1]');

but I need it too but something like

$target = $xml->xpath("//gig[@id=$" $change . "]");

Is this possible ?

If so could some one help ?

1
  • 1
    What is the question? You provided (except for the missing . typo) the solution yourself. Commented Jan 21, 2010 at 13:02

1 Answer 1

6

Use:

$target = $xml->xpath("//gig[@id=$". $change . "]");

or, if you do not need the $ sign

$target = $xml->xpath("//gig[@id=". $change . "]");

If you need to escape a character like " in php use

\"
Sign up to request clarification or add additional context in comments.

4 Comments

$target = $xml->xpath("//gig[@id='{$change}']") is also valid.
Note that unless the content of $change is known, the above code is prone to injections. An attacker can cause arbitrary nodes to be returned ($change = "some_nonsensical_id'] | a malicious xpath | //some_nonsensical_element_name[@some_nonsensical_attribute_name='").
@Witiko Isn't that the same for the premise in general, interpolating a variable in a string? It is a genuine question I don't claim to know
Not every string is an SQL command. If you are constructing a string containing natural language text, you may not care too much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.