This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
Closed 7 years ago.
See the following issue:
$str = "video-23984"; // returns false
$str = " video-23984"; // returns true
$search= "video";
if(strpos($str,$search)) {
echo "True";
}else {
echo "False";
}
Why in the world does $str = "video-23984" return false? And what can I do to make it return true?
In the first string the word video is in the first position, which means 0.
In the second it's in the second position, which means 1.
Since you're returning it into an if, the 0 is a boolean for false. That's why you're getting false as a result.
Related
This question already has answers here:
Case insensitive string comparison
(6 answers)
Closed 1 year ago.
Question:
if ("sometext" == "SOMEtext") {
echo "TRUE";
} else {
echo "FALSE";
}
It is return me FALSE, when I use double equal.
The only diference are the capital leters...
This if should return TRUE ????
You can use strtolower("somestring") in order to compare 2 strings without account for case sensitivity
This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
If string contains forward slash
(4 answers)
Closed 5 years ago.
i try to check a string with strpos().
$a = $_SERVER['REQUEST_URI'];
This works just fine
if (strpos($a, 'en' ) == true)
But this doesn't
if (strpos($a, '/en/' ) == true) - Doesn't work
I tried a lot of things to escape the character or format the string but as it seems I am too stupid...
The issue is that strpos returns the position or FALSE if not found.
So if the url is /en/something/some then you are hitting the situation where en is a position 1 and any non-zero number is true
When you do /en/ then the starting position is 0 and that is false.
you need to check with === in the end or more accurately !== example
<?php
$a= "/en/blo";
if (strpos($a, '/en/' ) !== false){
echo "TRUE";
} else {
echo "FALSE";
}
This question already has answers here:
PHP - if condition inside string
(5 answers)
Closed 8 years ago.
I have a string like this:
$str = "0 || 0 && 1";
actually this string is a condition.
if i do like this :
if($str) {
echo "done";
}
else {
echo "sfcsd";
}
it is always true since $str is string.
How can i evaluate this string with out eval().
check out sandboxing in PHP. here is a quick tutorial:
http://www.fieryprophet.com/blog/detail/sandboxing-untrusted-code-with-phpsandbox
This question already has an answer here:
symbol and decimal number true in php
(1 answer)
Closed 8 years ago.
I have script like this
if('#' == 0){
echo "true";
}else{
echo "false";
}
output :
true
the question is why it get true and how to get it false?
thanks.
As Mark Baker stated in the comments, PHP uses weak typing. According to the comparison matrix that you find here (http://www.php.net/manual/en/types.comparisons.php), the expression involving the string '#' compared to 0 will be evaluated as true.
Change your condition to:
if('#' === 0){
...
in this case you'll get false.
For this condition to get true of false properly, you need to compare the value and the type of the operators. Try this:
if('#' === 0){
echo "true";
}else{
echo "false";
}
You will get false for this case.
This question already has answers here:
startsWith() and endsWith() functions in PHP
(34 answers)
Closed 9 years ago.
How do one determine,if the strings appears at the end of the other string. If they do then print true to standard out and false if they don’t. Would strpos will help?
Sample Input
S1: “staff”
S2: “FF”
how would i make a function to run this,
function matching_ends($s1,$s2){
}
<?php
$s1="testing";
$s2="ing";
echo matching_ends($s1, $s2);
function matching_ends($s1,$s2){
return substr($s1, -strlen($s2)) == $s2 ? "true" : "false";
}
?>
Demo
You can use this
if( substr($s1, strlen($s1)-1) === substr($s2, strlen($s2)-1))
{
// Do something when character at the last matches
}
else{
// Do something when doesn't match
}