if( strpos($_SERVER['REQUEST_URI'], '/') === TRUE) doesn't work [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if( strpos($_SERVER['REQUEST_URI'], '/') === TRUE) doesn't work.
What I am trying to is achieve that when on the homepage e.g. http://www.amazon.co.uk/ that it will trigger what is inside the statement.
This may be complicated by the fact this is a redirect from another page
var_dump($_SERVER['REQUEST_URI']); = /string(1)
echo $_SERVER['REQUEST_URI']; = "/"
php version 5.4
Thank you for your help, I have fixed my issue

That is because, like the docs tell you, strpos() never returns true.
It either returns a positive integer (found) or boolean false (not found).
So check for !== false.

The strpos() function, returns a numeric value representing the position of the second string in the first string. Hence it will never be equal to TRUE.
strpos() can return FALSE if the string isn't found, hence you should be writing
if( strpos($_SERVER['REQUEST_URI'], '/') !== FALSE)

strpos() from manual:
strpos — Find the position of the first occurrence of a substring in a
string
So you are checking if the return is TRUE, and it never is. You need to check it with !== FALSE. Why? Because === checks for the value AND the type of variable. When you do == it will check only for value, and when value is 0 PHP will cast it to FALSE, since PHP is loosely typed.

Related

Checking to see of two DateTime objects refer to the same day [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to see whether two DateTimeImmutable objects refer to the same calendar day, and I can't figure out why the code below does not work. Code is PHP.
function isSameDay( $x, $y ){
echo "<p>Comparing '" . $x->format('Y-m-d') . "' and '" . $y->format('Y-m-d'). "' returns";
if( $x->format('Y-m-d') == $y->format('Y-m-d)') ){
echo " true </p>";
return true;
}
echo " false </p>";
return false;
}
When testing, I'm getting the following output, where the top test case should be returning true:
Comparing '2021-09-06' and '2021-09-06' returns false
Comparing '2021-09-06' and '2021-10-29' returns false
Comparing '2021-09-06' and '2021-11-24' returns false
Comparing '2021-09-06' and '2021-11-26' returns false
My understanding is that DateTimeImmutable->format() returns a string, so the if statement is just a string comparison. In fact, the compare-as-strings approach seems to be one of the main approaches for comparing dates like this. For example, the comparison I use above is given as answers to several questions here:
What is the best way to find out if 2 dates are in same month in PHP?
Check if two PHP datetime objects are set to the same date ( ignoring time )
Sigh, nevermind. There's an extra parentheses in $y->format('Y-m-d)')

Why does while(0) not execute the code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
<?php
$efFect=0;
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while ($str_position =strpos($string22,$find,$effect)){
echo $find.'the postion in '. $str_position.'<br>';
$efFect = $str_position + $find_len ;
}
?>
0 is a falsy value. You should make your check more reliable by checking if strpos actually returns false.
<?php
$effect=0; // not $efFect as variables names are case sensitive in PHP
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while (($str_position = strpos($string22, $find, $effect)) !== False){
echo $find.'the postion in '. $str_position.'<br>';
$effect = $str_position + $find_len ;
}
?>
From the documentation:
Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
Returns FALSE if the needle was not found.
Someone mentioned that your code creates an infinite loop but this is not the case. strpos accepts an offset as the third parameter which you have correctly used. On each iteration the offset is incremented such that the new search will begin at the position where the last found result string ends, hence avoiding the infinite loop.

The statement "if this OR that do X" won't work in PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to show some information on screen only if a certain field is not null or if it doesn't say the word "null".
I'm working with a PHP script.
For some reason I have a database that has some values set as null as a string, instead as a real NULL value. As there are many items and I still don't know what causes to set the field as "null" instead of NULL, it's going to be easier to just set that with an if statement for the time being.
But for some reason, this is not working:
if ($row['cronograma'] != NULL || $row['cronograma'] != 'null') {
echo 'This is my reply';
}
If the field is actually NULL, the echo won't show up. If the field has the string null in it it does show up.
Please note that the fields in question are either NULL or has the "null" string without any spaces.
It looks like you're after and rather than or. That is, if the value is not null AND is not the string "null":
if ($row['cronograma'] != NULL && $row['cronograma'] != 'null') {
// ^^
You are looking for "and" rather than "or" logic. Also, you should always use strict equality (triple equals) when comparing with NULL to prevent weird edge cases from popping up:
if ($row['cronograma'] !== NULL && $row['cronograma'] !== 'null') {
echo 'This is my reply';
}

PHP - If statement always evaluating as true? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I can't get my head around this if statement.
What I am trying to do is get the person logged in from $_SESSION["user_name"] and if it matches one of two possible "admin" users, to give extra functionality. But it's always firing true.
For the example I'm going to have three users: Emma, John and Robert. Emma and John should be allowed the extra "admin functionality", but Robert is not allowed it. So...
My statement is:
if ($_SESSION["user_name"] === "emma" or "john"){
//give extra functionality as well as basic functionality
} else {
//give just basic functionality
But this is always firing true, so when I log in as my test account with the username Robert, he is also getting the extra functionality.
I've tried changing the quotation marks " of $_SESSION to apostrophes ', and also tried changing the operator from === to ==. I even tried = but found the hard way that this was setting my $_SESSION variable to "emma".
What am I doing wrong because I just can't seem to get my head around it?
If it's worth noting, this if statement is contained in a parent if statement that uses colons : and endif rather than brackets {}. The parent if statement is purely there to decide on what functionality to output based a column returned being empty or a user's name in there.
You need to redeclare the full condition:
if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john"){
//give extra functionality as well as basic functionality
} else {
...
}
Update With Explanation:
The pseudo code syntax for if statements is:
if ([some condition] [and/or] [another condition]) {
// then do some stuff
}
each of those [condition] statements is evaluated as either "truthy" or "falsey". So your original question could be re-written something like this:
if ([$_SESSION["user_name"] is equal to "emma"] or ["john" is not false, 0, or null]) {
// we will always get in here
}
Since "john" will always be "truthy" (it is not false, null, 0) it will always pass the condition.
The term "john" as an expression is true:
if ( "john" )
will evaluate to true and not throwing any syntax errors.
What you want is:
if ($_SESSION["user_name"] === "emma" || $_SESSION["user_name"] === "john") {
It may help to break this into steps. PHP will evaluate everything to the left of or to see if it's true or false. Then it will evaluate what's on the right of the or.
Turns out, "john" evaluates to true.
You're looking for:
if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john") {
Try something like this
if (($_SESSION["user_name"] === "emma") || ($_SESSION["user_name"] === "john"))
{
//do something
}
else
{
//other thing
}

Are this if statements equal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i came across a php statement and was thinking, is
if(isset($_SESSION['id'])==1){
}
very similar to
if (isset($_SESSION['id']) && $_SESSION['id']==1){
}
Let us break it down.
isset($_SESSION['id']) == 1
The first part, the isset() call, will return a boolean value (true or false), depending on if the session id is set or not.
When you compare an integer value with a boolean value, using the == operator, the integer will be coerced (or cast/type-juggled) into a boolean value. So, if the integer is a "truthy" value, i.e. greater than or equal to 1, then it will be turned into true. If it is lower than or equal to 0 then it will be turned into false.
In other words, if the session is set then the expression will be turned into true === true (which evaluates to true). If it is not set then it will be turned into false === true (which evaluates to false).
So, in conclusion, it probably does not do what you thought it did. Use the latter if statement.
The statements are not equivalent. Because isset() returns either TRUE or FALSE, the first test:
if(isset($_SESSION['id'])==1){
}
is exactly equivalent to:
if(isset($_SESSION['id'])){
}
because php converts 1 to the Boolean value TRUE so the == operator has compatible types. The second, on the other hand, not only test whether $_SESSION['id'] is set, but also, if it is, whether the value is set to 1 (after type juggling to resolve comparing a string with an integer).

Categories