Include 'x'?

I like to find whether $myVar has “x” or not.
The would-be code below doesn’t work correctly but I hope it tells what I want more clearly.

[b]would-be code1[/b]
$myVar='abcd';

if(include('x',$myVar)){
echo 'yes';
}else{
echo 'no';
}
[b]result1[/b]
no

[b]would-be code2[/b]
$myVar='wxyz';

if(include('x',$myVar)){
echo 'yes';
}else{
echo 'no';
}
[b]result2[/b]
yes

The PHP include command is for including external files into the current code.

If you want to know if a string contains a particular character then strpos or strstr might be a more suitable call.

Use another variable inside included file

$myVarIncluded = 'abcd';

Check variables in main file



$myVar='abcd';
include('x');
if($myVar == $myVarIncluded){
echo 'yes';
}else{
echo 'no';
}

@ugniesdebesys; either one or both of us must be in need of a break, but what is that?

Unless by “x” you mean “the included file’s path / name” it has ERROR written all over it.

It will work just fine if you have a file named ‘x’ in the same path in as the PHP file. PHP files are not required to have the .php extension.

That being said, doing it this way is a very Bad Idea ™, since the file ‘x’ could possibly be accessed via a browser without PHP parsing it, so the server would just serve up the plain text, which may include stuff like database passwords etc. Plus, ‘x’ is about as undescriptive a filename as I can think of :wink:

</ever-so-slightly-pedantic>