Tested in PHP 5.3.10 on CentOS.
In a script a run:
$test = "62 3/4";
if($pos = strpos($test,' ') !== false) {
$test= substr($test,0,$pos); // use $pos
}
// $test is "6"
And in another independent script I run:
if($pos = strpos($test,' ') !== false) {
$test = substr($test,0,strpos($test,' ')); // redo substr calculation
}
// $test is "62"
$pos should be 2 (the third character is a space, starting from zero, 0,1,2), so both $test should be "62", no?
Operator precedence! !== comes before =, so the test effectively becomes
if($pos = (strpos($test,' ') !== false))
which is going to evaluate to either true or false, not the string position.
Always use explicit parens:
if(($pos = strpos($test,' ') !== false)
You want
if( ($pos = substr($test, ' ')) !== false ) {
// ...
}
See PHP Operator Precedence
Also consider that in your second if, the value of the variable $test was already changed by the first: $test= substr($test,0,$pos);
Related
Seems super trivial, but can't find a solution to this specific case on SO
A function may return a value of 0 OR another number, which I then want to store in a variable $s to calculate stuff. But can't find a clean way of doing it.
So for example:
function f() {
$v = "0";
return $v;
}
if($s = f() !== false) {
echo $s;
// ^ I want 0, but the value above is 1 (since it's true)
}
I tried returning it as a string
return "0" instead of a digit, but it doesn't work.
If I do this it will not evaluate to true so nothing will happen
if($s = f()) {
// returns nothing
}
But when I var_dump(f()), it does show string '0' (length=1)
So I can do
if(var_dump(f()) == 0)
OR
if(f() == 0)
But is there not a cleaner way to do it, so the function may return 0 or another number and I can just capture it in a variable?
Add parentheses:
if (($s = f()) !== false) {
Otherwise you're computing the value f() !== false and assigning it to $s, instead of assigning f() to $s and then comparing to false.
First
if($s = f() !== false) {
echo $s;
// ^ I want 0, but the value above is 1 (since it's true)
}
Whats happening here is return value of f() is strictly compared to false, that is, "0" is compared to false. They are not strictly equal, hence f() !== false returns true, which is stored in $s as 1.
Next,
if($s = f()) {
// returns nothing
}
This doesnt enter the if block because $s contains "0" and it is loosely equal to false.
Next
if(var_dump(f()) == 0) or if(f() == 0)
this works because it is loosely comparing "0" to 0 which is true. Hence it enters the block.
Remember, if condition does loose comparision or == and === does strict comparision. More about it here.
So, this should work in your case
if(($s = f()) !== 0)
if f() returns integers
Try some parenthesis:
if(($s = f()) !== false) {
To force $s to equal f(), not f() !== false
im trying to check if two php-values are in one line in the csv fiel.
csv:
password, name
12345, max
44444, emil
but when my variable is $password="123" and in the csv file its "12345" he accepts it.
But, how can i check if its 100% equals? I dont understand why "123" is enough?
$search = $name;
$search2 = $password;
$lines = file('Benutzer.csv');
$line_number = false;
$line_number2 = false;
while (list($key, $line) = each($lines) and !$line_number) {
$line_number = (strpos($line, $search) !== FALSE );
$line_number2 = (strpos($line, $search2) !== FALSE );
}
if($line_number and $line_number2 ){
header('Location: alert_anmelden_erfolgreich.php');
}
else{
header('Location: alert_anmelden_NICHT_erfolgreich.php');
}
strpos looks for a substring in a string. So first things first"
strpos('12345','345')
will return 2 since the substring exists starting at that index. In your case:
strpos('12345','123')
or really 1,12,123,1234,12345 will all return the position 0. Now, when equating as you do to false you're essentially getting:
0==`false`
which is of course true because 0 an be casted to false. As #u_mulder, commented use of the full type equality operator === would fix that, but still won't solve your problem!. What you want is strcmp, which will return 0(false) only if the strings are identical, and can be used like you wanted. You could also use === or == between the strings since you don't care about 'less/more' string.
if( $line === $search )
This is my code to check if a row of my .csv file contains a specific name, but it does not work.
I think it has something to do with the if statement.
$file_handle = fopen("sources.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[0] = 'paul') {
echo 'Found';
}
}
fclose($file_handle);
I am trying to check in sources.csv files, for the name 'Paul' .
I can't use a database like MySQL for technical reasons.
Since you haven't provided a sample of your file, am submitting the following as an alternative.
Do note that in your present code, you are assigning using a single equal sign = instead of comparing using == or ===, just saying as an FYI.
if ($line_of_text[0] = 'paul') should read as if ($line_of_text[0] == 'paul')
Assuming the following .csv format (will work even without the commas) and is case-sensitive, consult Footnotes regarding case-insensitive search.
Paul, Larry, Robert
Code:
<?php
$search = "Paul";
$lines = file('sources.csv');
$line_number = false;
while (list($key, $line) = each($lines) and !$line_number) {
$line_number = (strpos($line, $search) !== FALSE);
}
if($line_number){
echo "Results found for " .$search;
}
else{
echo "No results found for $search";
}
Footnotes:
For a case-insensitive search, use stripos()
$line_number = (stripos($line, $search) !== FALSE);
Row number found on...
To give you the row's number where it was found:
$line_number = (strpos($line, $search) !== FALSE) ? $count : $line_number;
or (case-insensitive)
$line_number = (stripos($line, $search) !== FALSE) ? $count : $line_number;
then just echo $line_number;
Your problem is this line:
if ($line_of_text[0] = 'paul') {
This will always be true because you are assigning the value paul to $line_of_text[0] with the assign operator =. What you want to do is check if the two are equal, so you need to use the equality operator, ==. The line of code should be:
if ($line_of_text[0] == 'paul') {
There is also the === equality operator in PHP which checks for the same value AND same type. (This is a particularly nasty feature of PHP when compared to compiled languages)
e.g. consider: `$foo = 5; $bar = "5";
if ($foo === $bar) // this will be false because foo is an int, and bar is a string
if ($foo == $bar) // this will be true
Don't be confused with the != comparison operator:
if ($foo != $bar) // foo is not equal to bar (note the single =)
Try using in_array instead of ==
if(in_array('paul', $line_of_text)) {
// FOUND
}
This works:
if (strpos($page, 'http') == 0) {
$link = 'Test';
}
But this doesn't:
if (strpos($page, 'http' || 'www' || '/') == 0) {
$link = 'Test';
}
I need to return something if $page does not begin with any of those three: http, www, or /.
if (strpos($page, 'http') == 0 || strpos($page, 'www') == 0 || strpos($page, '/') == 0) {
$link = 'Test';
}
you cannot use || like that.
Other than the 'bad arguments' answers above, you've also got a serious logic bug. strpos can and WILL return a boolean FALSE if the 'needle' string isn't found in the 'haystack'. e.g.
$needle = 'foo';
$haystack = 'bar';
if (strpos($haystack, $needle) == 0) {
echo 'found it!';
}
will say found it!, because strpos returned boolean FALSE, which PHP then typecast over to an int to compare to the 0. (int)FALSE becomes 0.
You need use the strict comparison operator === to make sure you really are comparing int to int, and not int to possibly-boolean-false:
if (strpos(...) === 0) { ... }
Unfortunately PHP doesn't understand "If the door is red or green or blue". You have to spoon-feed it "if the door is red or the door is green or the door is blue". But there's still some shortcuts you can take:
if( preg_match("(^(?:http|www|/))",$page)) $link = "Test";
I recommend using regular expressions for a case like this when you need to do pattern matching. More efficient in every way and way easier once you get the gist of it. This is a very helpful guide: http://oreilly.com/catalog/regex/chapter/ch04.html
I found this example on stackoverflow:
if (strpos($a,'are') !== false) {
echo 'true';
}
But how do I make it search for two words. I need something like this: if $a contains the words "are" or "be" or both echo "contains";
I tried xor and ||
Just check both words separately and use the boolean or-operator to check if either one or both are contained in $a:
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
echo "contains";
}
Note that due to the or-operator, the second check (for 'be') is not performed if the first one already showed that $a contains 'are'.
An alternative: Searches any length of words in the longer string.
Since you've haven't picked an answer from all the strpos answers (most of which should work with just two words, try this function of mine which exceeds word limits. It can find any varying length of words from the longer string (but doesn't use strpos). I think with strpos, you would have to know the number of words to determine how many || you should use or make use of a loop (sort of). This method eliminates all that and gives you a more flexible way to reuse your codes. I thinks codes should be flexible, reusable and dynamic. Test it and see if it does what you want!
function findwords($words, $search) {
$words_array = explode(" ", trim($words));
//$word_length = count($words_array);
$search_array = explode(" ", $search);
$search_length = count($search_array);
$mix_array = array_intersect($words_array, $search_array);
$mix_length = count($mix_array);
if ($mix_length == $search_length) {
return true;
} else {
return false;
}
}
//Usage and Examples
$words = "This is a long string";
$search = "is a";
findwords($words, $search);
// $search = "is a"; // returns true
// $search = "is long at"; // returns false
// $search = "long"; // returns true
// $search = "longer"; // returns false
// $search = "is long a"; // returns true
// $search = "this string"; // returns false - case sensitive
// $search = "This string"; // returns true - case sensitive
// $search = "This is a long string"; // returns true
$a = 'how are be';
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
echo 'contains';
}
Try:
if (strpos($a,'are') !== false || strpos($a,'be') !== false)
echo 'what you want';
if ((strpos($a,'are') !== false) || (strpos($a, 'be') !==false) {
echo 'contains';
}
Is this what you want?
if ((strpos($a,'are') !== false) || (strpos($a,'be') !== false)) {
echo 'contains';
}
if (strpos($a,'are') !== false || strpost($a, 'be') !== false) {
echo "contains";
}
Brain Candy:
If the first one returns true, it'll skip the second check. So both can be true. If the first one is false, ONLY then will it check the second. This is called a short circuit.
if(strstr($a,'are') || strstr($a,'be')) echo 'contains';
Hum, like this?
if (strpos($a,'are') || strpos($a, 'be') {
echo 'contains';
}