I am setting a session variable in a php file as:
$_SESSION['PageFrom']="check_login_test.php";
Later on the control is transferred to another php file via a form and post. The relevant code in the 2nd file is:
session_start();
$from=trim($_SESSION['PageFrom']);
pageFrom("check_login_test.php",$from);
die('I came here');
Now pageFrom is a function that should have compared the strings and said so. The original function was simply
function oldPageFrom($page,$from)
{
$page= trim($page);
$from=trim($from);
if (strcmp($page,$from)!==0)
{
echo 'The pages are not same';
}
}
Since it did not work as expected, I tried to debug the same and output as much as I can (This changed the old function, but now reveals some things interesting) . The function is
function pageFrom($page,$from)
{
echo '<br/>$page='.$page;
echo '<br/>$from='.$from;
$m=trim($page);
$n=trim($from);
echo "<br/>TrimmedPage=$m<br/>TrimmedFrom=$n";
$k= strcmp($m,$n);
echo '$k='.$k;
if($k !==0);
{
$i=strlen($from);
$j=strlen($page);
if ($i==$j)
{
echo "<br/>The string lengths are Equal $i=$j";
die('Equal in unequal');
}
else
{
echo "<br/>The string lengths are UnEqual $i<>$j";
die('UnEqual in unequal');
}
}
echo 'Works as expected. They are equal'; die('equal');
}
Strangely I get the following as output:
$page=check_login_test.php
$from=check_login_test.php
TrimmedPage=check_login_test.php
TrimmedFrom=check_login_test.php$k=0
The string lengths are Equal 20=20Equal in unequal
So the questions are:
(1)I have trimmed the strings. They are equal and of the same length. Strcmp returns 0. Then why does it enter the loop if($k !==0) at all? Note: no luck in ($k!=0) too.
You're using the following code:
if($k !==0);
{
The semi-colon (;) terminates the if clause and the subsequent { } block won't have any effect. Remove the ; after if and it will work as it should, i.e. this code below
if($k !==0)
{
Related
I have a question about php (wordpress) I have a plugin(buddypress) with function bp_the_profile_field_name();
when i use
echo bp_the_profile_field_name();
It's return "Tags"
But..
if (bp_the_profile_field_name() == "Tags"){
echo "Yes, it's working";
} else {
echo "Oh no..";
}
?>
its return "Oh no.."
When i try equal to string "Tags" dosent't match.
Why? Please help
When googling to the source of the bp_the_profile_field_name function I found this source:
function bp_the_profile_field_name() {
echo bp_get_the_profile_field_name();
}
Here we can see that the the function uses echo to show the value.
If you want to get the value to compare it with something else you'll need to use a other function which will return the value.
Below the mentioned page in the 'related' part this function is mentioned:
bp_get_the_profile_field_name
Returns the XProfile field name.
Note the _get_ insteaf off _the_
Which returns a string. Source can be found here.
So your code should become:
if (bp_get_the_profile_field_name() == "Tags"){
echo "Yes, it's working";
} else {
echo "Oh no..";
}
According to documentation (https://www.buddyboss.com/resources/reference/functions/bp_the_profile_field_name/) bp_the_profile_field_name() echoes output.
If you want to compare the output of this function you need a return, not echo.
You have 2 options:
Use bp_get_the_profile_field_name() instead of bp_the_profile_field_name() in your if statement
Edit bp_the_profile_field_name() to output the result instead of echoing it.
I have been making a Fahrenheit to Celsius (and vise versa) calculator. All of it works just great, however when I try to calculate 32 fahrenheit to celsius it's supposed to be 0, but instead displays nothing. I do not understand why it will not echo 0 values.
Here is some code:
<?php
// Celsius and Fahrenheit Converter
// Programmed by Clyde Cammarata
$error = '<font color="red">Error.</font>';
function tempconvert($temptype, $givenvalue){
if ($temptype == 'fahrenheit') {
$celsius = 5/9*($givenvalue-32);
echo $celsius;
}
elseif ($temptype == 'celsius') {
$fahrenheit = $givenvalue*9/5+32;
echo $fahrenheit;
}
else {
die($error);
exit();
}
}
tempconvert('fahrenheit', '50');
?>
looks like $celcius has value 0 (int type) not "0" (string type), so it wont echoed because php read that as false (0 = false, 1 = true).
try change your code
echo $celcius;
to
echo $celcius."";
or
echo (string) $celcius;
it will convert your variable to string
when it printed nothing, could you have had a typo in the temptype 'fahrenheit'?
The code matches temptype, and if it's not F or C it errors out. Except that $error is not declared global $error; which uses a local undefined variable (you must not have notices enabled which would warn you), and undefined prints as the "" empty string.
die() is equivalent to exit(). Either function with the single parameter as an integer value of 0 indicates to PHP that whatever operation you just did was successful. For your function, if you want the value 0 output, you would want to use echo or print, not die() or exit().
Further, consider updating your function to return the value instead of directly outputting it. It will make your code more reusable.
More info about status codes here:
http://php.net/manual/en/function.exit.php
echo (float) $celcius;
This will do the work for you.
I run this code:
<?
$map = do_shortcode('[codepeople-post-map]');
if (empty($map)) {echo '<br />'; } else {echo $map;}
?>
The problem is when the $map is empty it won't echo the <br />
I have tried this 2 methods to see if $map is really empty:
echo $map; // It results empty and noting is shown on page.
var_dump($map); // It results: string(937) " "
My question is what does string(937) " " means and how can I make my code work?
I tried also:
<? if ($map == string(937) " ") {echo '<br />'; } else {echo $map;} ?>
But no success so far this last code is wrong and just gives error.
A variable is considered empty if it does not exist or if its value equals FALSE
Your variable is a string with a space, which evaluates to TRUE, so it is not empty.
Instead of using empty(), you might check if trim($map) === ''.
Well, it means that you have a string with a space in it.
Probably the var was previously a string which was emptied wrong. One solution I could suggest you, depending what your application's purpose is.
if(trim($map) == "" || $map == null)
{
echo "<br />";
}
else
{
echo $map;
}
Because actually it only means that the string contains a whitespace (this explains why there is a space in " "). You should try to locate where you get the $map value and try to put some verifications there.
Alex
I have encountered a very weird and concerning problem in some of my PHP code. A variable that I have returns true in an IF statement when it clearly should return false.
$pr = $_SESSION['fin_print_printer']; //this should equal 0
print $pr; //this returns 0, as it should
if($pr == "L"){
print "local";
} else {
print "serve";
}
print $pr; //this returns 0 again, as it should
This prints "local" in my script (in between the two zeros) and does not print "serve". With over 100,000 lines of code in my project, I've not experienced this issue yet, and now I can't figure out what is going on.
If I do if($pr === "L"), then it works as expected, but the above does not.
PHP is trying to typecast 'L' into an int, which results in 0.
intval('L'); // 0
Change your code into the following so it will take types into account:
if($pr === "L")
{
print "local";
}
else
{
print "serve";
}
Or manually typecast $pr to a string.
// You can also to (string)$pr ("0" instead of 0)
if(strval($pr) == "L")
{
print "local";
}
else
{
print "serve";
}
Maybe if you use typecasting (I did't check it):
if ( (string)$pr == "L" ) {
print "local";
} else {
print "serve";
}
Little known approach: You can also do the casting like
if ("L" == $pr) {
Because with loose comparisons, PHP casts the right value to the left value's type, and as you've already been made aware, while string(1)"L" is casted to int(0), int(0) is casted to string(1)"0".
I dont really understand why this is not working, I am being returned to the correct header but the item is still in the array!
Here is the remove from array code:
<?php
session_start();
if ( !isset($_SESSION['username']) )
{
header("Location:loginform.php");
exit();
}
foreach ($_SESSION['list'] as $key => $disk)
{
if (($_SESSION['list'][$key]['bookisbn']) - ($_GET['bookisbn'])== 0)
{
unset($_SESSION['list'][$key]);
break;
}
}
header("Location: ".$_GET['location']);
exit();
?>
Thank you for any help you can offer
This is only a guess but the problem may lie in the fact that
$_GET['bookisbn']
is being treated as a string. Therefore if you cast it to an int
the if statement will return true removing the item from the
array.
Introduce this code:
// Casting the ISBN to an integer here
$bookISBN = (int) $_GET['bookisbn'];
if( ($_SESSION['list'][$key]['bookisbn'] - $bookISBN) == 0 ) {
// Unset item
}
I would listen to Artragis, but also try this:
You have parenthesis around each of the variables, but it's really the subtraction that has to equal zero. So, instead of:
if (($_SESSION['list'][$key]['bookisbn']) - ($_GET['bookisbn'])== 0)
{
unset($_SESSION['list'][$key]);
break;
}
Try:
if (($_SESSION['list'][$key]['bookisbn'] - $_GET['bookisbn']) == 0)
{
unset($_SESSION['list'][$key]);
break;
}
EDIT:
I would also cast both the Session and the GET variables as INTs, like one of the other posters mentioned, as well.
Try session_write_close() before the header. Perhaps is not saving.
You can also do this:
$mydata = $_SESSION['list'] ;
//do something with $mydata
$_SESSION['list'] = $mydata;
It will result in easier to read code, if you have other type of error, with this way you will not make the same mistake again.
You need to debug and test if line with unset is reachable.
You need to debug means you need to verify every program's step and every variable.
You need to test if line with unset is reachable means you have to echo something at the point of unset:
foreach ($_SESSION['list'] as $key => $disk)
{
var_dump("---\n",$_SESSION['list'][$key]['bookisbn'],$_GET['bookisbn']);
var_dump($_SESSION['list'][$key]['bookisbn'] - $_GET['bookisbn']);
var_dump($_SESSION['list'][$key]['bookisbn'] - $_GET['bookisbn'] == 0);
if ($_SESSION['list'][$key]['bookisbn'] - $_GET['bookisbn'] == 0)
{
unset($_SESSION['list'][$key]);
var_dump($_SESSION['list']);
break;
}
}
//comment out header to prevent moving out of page
#header("Location: ".$_GET['location']);
exit();
run this code and see if any of the values are wrong or unexpected.