function check($text){
if(strpos('a', $text) == FALSE && strpos('b', $text) == FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
live: http://codepad.org/W025YYuH
why this not working? This return:
1 error 2 error 3 error 4 error 5 error
but should be:
1 error 2 ok 3 ok 4 error 5 ok
You should use === FALSE instead of == FALSE, as explained in the documentation
Additionally, your arguments are in the wrong order. Again, consult the documentation (or, as some people say, RTM)
Invert the position of the argument, first argument is the string, second argument is what do you search into the string.
strpos ( 'The string to search in' ,'the argument of search' )
Then == would not work as expected
because the position of 'a' was the 0th (first) character.
Try this:
function check($text){
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
You have the arfuments the wrong way around, change to:
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
Also note that you need to check for a boolean false with the identical operator (===)
Related
I want to loop through a set of conditions, returning true only if every condition is met, collecting reasons along the way if not.
<?php
$dataval = 0;
$tests = [
[1,0,0,4,5],
[0,0,0,0,0]
];
foreach($tests as $condition) {
$retval = null;
$reasons = [];
foreach($condition as $item){
if($item == $dataval){
$retval == $retval && true;
} else {
$retval == $retval && false;
$reasons[] = "Failed to match " . $dataval . " to " . $item;
}
}
if($retval === true){
echo "All conditions met<br>";
} else {
echo "NOT all conditions met<br>";
}
echo "<pre>" . print_r($reasons, 1) . "</pre>";
}
?>
OUTPUT
NOT all conditions met
Array
(
[0] => Failed to match 0 to 1
[1] => Failed to match 0 to 4
[2] => Failed to match 0 to 5
)
NOT all conditions met
Array
(
)
No matter what the initial value of $retval, one or both tests is going to fail. If the initial value is true, both tests return true (which is incorrect); if false or null, then both return false (which is also incorrect).
Yes, I could break on the first false, but why the test failed is important, and it could fail for more than one reason so I shouldn't just break out of the loop as soon as the first failure is caught.
Is there a way to do this without adding another variable to tally up the hits and misses?
You need to initialize $retval to true. When you get a mismatch, set it to false and push the error onto the $reason array.
But don't really need the $retval variable. Just check if the array is empty.
foreach($tests as $condition) {
$reasons = [];
foreach($condition as $item){
if($item != $dataval) {
$reasons[] = "Failed to match " . $dataval . " to " . $item;
}
}
if(empty($reasons)){
echo "All conditions met<br>";
} else {
echo "NOT all conditions met<br>";
echo "<pre>" . print_r($reasons, 1) . "</pre>";
}
}
I have an php in a server and i found an issue (solved). The issue was in a comparison among to long integer saved in a string and I can not use Equal, I have to use Identical. But I do not know why I have to use it, if it is a comparison among to strings.
I make a test, and I get two different results, in local host and server.
Server PHP version: 5.3.10-1ubuntu3.10
Local PHP version: 5.6.30-7+deb.sury.org~xenial+1
Here is mi code:
<?php
$a = "1285615000003961035";
$b = "1285615000003961023";
if($a == $b)
{
echo "$a == $b<br>";
}
else
{
echo "$a != $b<br>";
}
if((string)$a == (string)$b)
{
echo "(string)" . (string)$a . " == (string)" . (string)$b . "<br>";
}
else
{
echo "(string)" . (string)$a . " != (string)" . (string)$b . "<br>";
}
if($a === $b)
{
echo "$a === $b<br>";
}
else
{
echo "$a !== $b<br>";
}
if((string)$a === (string)$b)
{
echo "(string)" . (string)$a . " === (string)" . (string)$b . "<br>";
}
else
{
echo "(string)" . (string)$a . " !== (string)" . (string)$b . "<br>";
}
?>
In Server I get:
1285615000003961035 == 1285615000003961023
(string)1285615000003961035 == (string)1285615000003961023
1285615000003961035 !== 1285615000003961023
(string)1285615000003961035 !== (string)1285615000003961023
and in local:
1285615000003961035 != 1285615000003961023
(string)1285615000003961035 != (string)1285615000003961023
1285615000003961035 !== 1285615000003961023
(string)1285615000003961035 !== (string)1285615000003961023
Since PHP 5.4, according to the release notes, integral strings that overflow into floating point numbers will no longer be considered equal, because of the way float numbers are represented internally.
For more information see the links below:
http://php.net/manual/en/language.operators.comparison.php (Warning section after the examples)
http://php.net/manual/en/types.comparisons.php#108264
You should use strcmp or === for comparing strings (as you have used), because when you use == PHP will do type conversions.
http://php.net/manual/en/language.operators.comparison.php
Edit: As Lucas Mendes stated above, this is only partially true after PHP 5.4 - but still it is safer to use operators that do not do type conversions by default to compare strings.
Not sure what im doing wrong here, but the out come is always null. The script should output "you did not select an answer" only if no answer was selected but otherwise it should output the answer given:
I have updated the script as mentioned but still getting the empty output even when answer is given :/
Thanks for all the help so far guys, but even the below code doesnt work, it now just outputs as blank if no anwser, but if you do fill it in, it correctly echos the answer.
if (empty( $a1 )) {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p>You did not select an answer</p>\n"
. "</li>\n";
}
else {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
. "<p>" . $r1[$a1] . "</p>\n"
. "</li>\n";
}
Completely forgot to show this part!!
// get local copies of single answers
$a1 = trim(isset($_POST['a1'])?$_POST['a1']:99);
$a3 = trim(isset($_POST['a3'])?$_POST['a3']:99);
$a4 = trim(isset($_POST['a4'])?$_POST['a4']:99);
$a5 = trim(isset($_POST['a5'])?$_POST['a5']:99);
Don't use if($a1 == null) use if(empty($a1)) or if(isset($a1))
An empty string is not null
$a1 = '';
if ($a1 == null) // is wrong
should be
$a1 = '';
if ($a1 === '')
or
if (empty($a1))
an empty is not the same as null try
if ($a === '') this respects also the type which is better for code quality
if (empty( $a1 )) {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p>You did not select an answer</p>\n"
. "</li>\n";
}
else {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
. "<p>" . $r1[$a1] . "</p>\n"
. "</li>\n";
}
Use empty instead of null checking
'null' is not same as false or ''.'null' is an object.
In PHP, empty string ($a) & empty array ($b) will return true if you test following express:
$a = ''; $b = array();
$a == null -> TRUE $b == null -> TRUE
also,
$a == 0 -> TRUE
So you should use '===' to test, or there's always unexpected result in your code.
I wrote this REALLY simple code:
$foo=false;
echo $foo;//It outputs nothing
Why? Shouldn't it output false? What can I do to make that work?
false evaluates to an empty string when printing to the page.
Use
echo $foo ? "true" : "false";
The string "false" is not equal to false. When you convert false to a string, you get an empty string.
What you have is implicitly doing this: echo (string) $foo;
If you want to see a "true" or "false" string when you echo for tests etc you could always use a simple function like this:
// Boolean to string function
function booleanToString($bool){
if (is_bool($bool) === true) {
if($bool == true){
return "true";
} else {
return "false";
}
} else {
return NULL;
}
}
Then to use it:
// Setup some boolean variables
$Var_Bool_01 = true;
$Var_Bool_02 = false;
// Echo the results using the function
echo "Boolean 01 = " . booleanToString($Var_Bool_01) . "<br />"; // true
echo "Boolean 02 = " . booleanToString($Var_Bool_02) . "<br />"; // false
I would like to know if some word is present in the URL.
For example, if word car is in the URL, like www.domain.com/car/ or www.domain.com/car/audi/ it would echo 'car is exist' and if there's nothing it would echo 'no cars'.
Try something like this. The first row builds your URL and the rest check if it contains the word "car".
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
I think the easiest way is:
if (strpos($_SERVER['REQUEST_URI'], "car") !== false){
// car found
}
$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
echo "Car here";
}
else {
echo "No car here :(";
}
See strpos manual
if( strpos( $url, $word ) !== false ) {
// Do something
}
worked for me with php
if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}
This worked for me:
// Check if URL contains the word "car" or "CAR"
if (stripos($_SERVER['REQUEST_URI'], 'car' )!==false){
echo "Car here";
} else {
echo "No car here";
}
If you want to use HTML in the echo, be sure to use ' ' instead of " ".
I use this code to show an alert on my webpage https://geaskb.nl/
where the URL contains the word "Omnik"
but hide the alert on pages that do not contain the word "Omnik" in the URL.
Explanation stripos : https://www.php.net/manual/en/function.stripos
strstr didn't exist back then?
if(strstr($_SERVER['REQUEST_URI'], "car")) {
echo "car found";
}
This must be one of the easiest methods right?
Have a look at the strpos function:
if(false !== strpos($url,'car')) {
echo 'Car exists!';
}
else {
echo 'No cars.';
}
Surely this is the correct way round....
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'mysql')) {
echo 'No mysql.'; //swapped with other echo statement
} else {
echo 'Mysql exists.';
}
Otherwise its reporting the opposite way it should...
Starting with PHP 8 (2020-11-24), you can use str_contains:
if (str_contains('www.domain.com/car/', 'car')) {
echo 'car is exist';
} else {
echo 'no cars';
}
You can try an .htaccess method similar to the concept of how wordpress works.
Reference: http://monkeytooth.net/2010/12/htaccess-php-how-to-wordpress-slugs/
But I'm not sure if thats what your looking for exactly per say..
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'car')) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
This seems to work.