Q:

Something weird happening with the casperjs/phantom js code (casper.exists() function)

Sample Code:
function myfunc(){
    ctxt = "this is the" + variable1 + "value";
    if (casper.exists(x('//p[@class="classname" and contains(text(), ctxt)]{
            //code1;
    }
}

When myfunc is called, the if block gets executed every time no matter whether ctext exists or not.
But if I hardcode the text instead of ctxt for example "abcde", then it's execution doesn't occur.
This is what I mean:
contains(text(), "abcde")

A:

There is probably a problem with the space around the position where variable1 is inserted to. You could try to define ctxt like this:
ctxt = "this is the " + variable1 + " value";

The exists call should look like this:
casper.exists(x('//p[@class="classname" and contains(text(), "'+ctxt+'")]'))

You need to fix the closing brackets: ]{ should be ]'))){.
The other thing is that you forgot to use the variable. Instead you just inserted the variable name (ctxt) into the XPath expression and not the variable value "'+ctxt+'".
If this still does not work, you can try to normalize the white space of the text:
casper.exists(x('//p[@class="classname" and contains(normalize-space(text()), "'+ctxt+'")]'))

