*Afternote*: I made sure my question was asked as clearly as possible. This post is divided into five sections: three scenarios, my own thoughts about the problem (analysis), and the final section which describes exactly what problem i need solved. Thankyou.
I am still new to PHP, having self-taught myself the language through trial and error. I just learned about the file operation functions, and it is giving me trouble:
I will use three scenarios to describe my problem. Scenario one works, and does not use an external text file. Scenario two does not work, and does not use an external text file. Scenario three, which impicates an external text file, does not work:
Scenario One:
test.php
//... (continuing from HTML)
<?php
$data = "andrew"; //Store "andrew" into $data
$name = "$data"; //Store $data into $name, making $name "andrew" as well
$test = "andrew"; //Store "andrew" into $test
echo "$test $name"; //Just to check later what the variables actually are
if ($test == $name) {
echo "yes"; //If the value of $name is "andrew" display "yes"
} else {
echo "no"; //If the value of $name is not "andrew" display "no"
};
?>
//... (continuing with HTML)
OUTPUT
andrewandrewyes
Scenario Two:
test.php
//... (continuing from HTML)
<?php
$data = array("new york","andrew","korea","monroe"); //A simple array with four entries
$name = $data[1]; //Extracting the second entry from $data, which should be "andrew"
$test = "andrew"; //Store "andrew" into $test
echo "$test $name"; //Just to check later what the variables actually are
if ($test == $name) {
echo "yes"; //If the value of $name is "andrew" display "yes"
} else {
echo "no"; //If the value of $name is not "andrew" display "no"
};
?>
//... (continuing with HTML)
OUTPUT
andrewandrewno
Scenario Three:
test.php
//... (continuing from HTML)
<?php
$data = file("test.txt"); //Put text.txt (shown below) into an array, $data
$name = $data[1]; //Put second entree of $data array (which should be "andrew") into $name
$test = "andrew"; //Store "andrew" into $test
echo "$test $name"; //Just to check later what the variables actually are
if ($test == $name) {
echo "yes"; //If the value from the text file is "andrew" display "yes"
} else {
echo "no"; //If the value from the text file is not "andrew" display "no"
};
?>
//... (continuing with HTML)
test.txt
new york
andrew
korea
monroe
OUTPUT
andrewandrew no
ANALYSIS
I thought that all three scenarios should output the same, but they do not.
>>The first example works normally: "andrew" is equal to "andrew" and therefore returns a "yes." Naturally, there are no spaces between any of the words.
>>The second example is different: "andrew" does not seem to equal to the "andrew" coming from the array, although both seem the same. Again, no spaces are present.
>>In the third example, again both variables are "andrew" but for some reason they are not equal. In addition, there is a space after the "andrew" that comes from the array (which was transferred from the external text file).
MY REQUEST
This is all the information I could give you. My request is for anyone to tell me how to get scenario three to output a "yes." I do not care about spaces (I included that information about spaces because I thought maybe it might have something to do with the problem). Just outputting a "yes" for scenario three.
If you really need more information, I will try to give you more, so please ask. Thankyou very much for your patience in reading this, and I will thank you further if you can help me through this problem.ok here it is.
scenario 1, works like it should
scenario 2, works for me like it should
scenario 3, works if you do one thing.
being that you took it out of a file it adds a space in it for some reason. I added trim() to the data[1] variable and it works like it should.
$name = trim($data[1]);Originally posted by scoutt
scenario 3, works if you do one thing.
being that you took it out of a file it adds a space in it for some reason...It's not a space that is added, it's the newline character (which is always returned when using non-binary reads from a text file).i have no idea what "trim()" is or torrent's "new line" stuff is. but i'll try it later: gotta go somewhere right now. thankyou very much! i'll reply lateri quickly tried it and it works great. thankyou very much! gotta run now.Originally posted by torrent
It's not a space that is added, it's the newline character (which is always returned when using non-binary reads from a text file).
I was close
and I even looked at that with special characters on, hmmm, don't know why it didn't hit me.
I am still new to PHP, having self-taught myself the language through trial and error. I just learned about the file operation functions, and it is giving me trouble:
I will use three scenarios to describe my problem. Scenario one works, and does not use an external text file. Scenario two does not work, and does not use an external text file. Scenario three, which impicates an external text file, does not work:
Scenario One:
test.php
//... (continuing from HTML)
<?php
$data = "andrew"; //Store "andrew" into $data
$name = "$data"; //Store $data into $name, making $name "andrew" as well
$test = "andrew"; //Store "andrew" into $test
echo "$test $name"; //Just to check later what the variables actually are
if ($test == $name) {
echo "yes"; //If the value of $name is "andrew" display "yes"
} else {
echo "no"; //If the value of $name is not "andrew" display "no"
};
?>
//... (continuing with HTML)
OUTPUT
andrewandrewyes
Scenario Two:
test.php
//... (continuing from HTML)
<?php
$data = array("new york","andrew","korea","monroe"); //A simple array with four entries
$name = $data[1]; //Extracting the second entry from $data, which should be "andrew"
$test = "andrew"; //Store "andrew" into $test
echo "$test $name"; //Just to check later what the variables actually are
if ($test == $name) {
echo "yes"; //If the value of $name is "andrew" display "yes"
} else {
echo "no"; //If the value of $name is not "andrew" display "no"
};
?>
//... (continuing with HTML)
OUTPUT
andrewandrewno
Scenario Three:
test.php
//... (continuing from HTML)
<?php
$data = file("test.txt"); //Put text.txt (shown below) into an array, $data
$name = $data[1]; //Put second entree of $data array (which should be "andrew") into $name
$test = "andrew"; //Store "andrew" into $test
echo "$test $name"; //Just to check later what the variables actually are
if ($test == $name) {
echo "yes"; //If the value from the text file is "andrew" display "yes"
} else {
echo "no"; //If the value from the text file is not "andrew" display "no"
};
?>
//... (continuing with HTML)
test.txt
new york
andrew
korea
monroe
OUTPUT
andrewandrew no
ANALYSIS
I thought that all three scenarios should output the same, but they do not.
>>The first example works normally: "andrew" is equal to "andrew" and therefore returns a "yes." Naturally, there are no spaces between any of the words.
>>The second example is different: "andrew" does not seem to equal to the "andrew" coming from the array, although both seem the same. Again, no spaces are present.
>>In the third example, again both variables are "andrew" but for some reason they are not equal. In addition, there is a space after the "andrew" that comes from the array (which was transferred from the external text file).
MY REQUEST
This is all the information I could give you. My request is for anyone to tell me how to get scenario three to output a "yes." I do not care about spaces (I included that information about spaces because I thought maybe it might have something to do with the problem). Just outputting a "yes" for scenario three.
If you really need more information, I will try to give you more, so please ask. Thankyou very much for your patience in reading this, and I will thank you further if you can help me through this problem.ok here it is.
scenario 1, works like it should
scenario 2, works for me like it should
scenario 3, works if you do one thing.
being that you took it out of a file it adds a space in it for some reason. I added trim() to the data[1] variable and it works like it should.
$name = trim($data[1]);Originally posted by scoutt
scenario 3, works if you do one thing.
being that you took it out of a file it adds a space in it for some reason...It's not a space that is added, it's the newline character (which is always returned when using non-binary reads from a text file).i have no idea what "trim()" is or torrent's "new line" stuff is. but i'll try it later: gotta go somewhere right now. thankyou very much! i'll reply lateri quickly tried it and it works great. thankyou very much! gotta run now.Originally posted by torrent
It's not a space that is added, it's the newline character (which is always returned when using non-binary reads from a text file).
I was close
and I even looked at that with special characters on, hmmm, don't know why it didn't hit me.