DevSael Blog. C, CPP, C++, PHP, .NET Programming BLOG! advanced, MySQL, MongoDB, RDBMS.

Browsing "Older Posts"



Q. PHP Session variable value is not transfering?

So, you are having trouble with php session variables! Well, Let see how can it being solved! If you didn't read about PHP Session variables then have a look on the manual!

Well let see what is happening...

PHP file: <test1.php>

<?PHP
session_start();

header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8");

//some code to determine $approvedToSet
$approvedToSet = true;
if(isset($approvedToSet))
{
$_SESSION["userName"] = 'MyName'; 
echo " Session variable is set." .$_SESSION["userName"];
} 
else { 
echo "User name is not set";} 

?>

PHP file: <test2.php>

<?PHP
echo $_SESSION["userName"];

ERROR:

Notice: Undefined index: userName in /var/www/xyz on line 30


[Solution:]

Correct Your file and ensure session_start(); function is on the top of your page.

PHP file: <test2.php>

<?PHP
session_start()
echo $_SESSION["userName"]; ?>

  1. Stop your apache2 or http server and start it again!
  2. Ensure session.save_path has been set.
  3. Must be Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening <?php tag before anything else. Also ensure there are no whitespaces/tabs before the opening <?phptag.
  4. After the header redirect, end the current script using exit(); (Others have also suggestedsession_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();).
  5. Make sure cookies are enabled in the browser you are using to test it on.
  6. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.
  7. Make sure you didn't delete or empty the session.
  8. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere.
  9. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
  10. Make sure your file extension is .php (it happens!).
  11. If you use a connection script, dont forget to use start_session(); at the connection too, had some trouble before noticing that issue.
iu

[SOLVED] PHP Session variable value doesn't retrieved to another page

By Game Changer → Friday, May 5, 2017


Hi guys!
Let's have a clear conception about {int} {float} and {double} Do you know? Precision is the main difference. First, I am gonna show you some practical example!

C Difference between float and double

A float has 23 bits of precision; 8 bits of exponent, and 1 sign bit. A double has 52. bits of precision; 11 bits of exponent, and 1 sign bit.

float x = 3.141592653589793238;
double z = 3.141592653589793238;
printf("x=%f\n", x);
printf("z=%f\n", z);
printf("x=%20.18f\n", x);
printf("z=%20.18f\n", z);

Gives you the output

x=3.141593
z=3.141593
x=3.141592741012573242
z=3.141592653589793116

.Net difference between int, float and double

When Float ~7 digits (then 32 b),  Double ~15-16 digits (then 64 b) and Decimal ~28-29 digits takes 128 bit!

float flt = 1F/3;
double dbl = 1D/3;
decimal dcm = 1M/3;
Console.WriteLine("float: {0} double: {1} decimal: {2}", flt, dbl, dcm);
Result :

float: 0.3333333
double: 0.333333333333333
decimal: 0.3333333333333333333333333333

You have seen, Decimals much higher precision and are usually used within financial applications that require a high degree of accuracy.
Okay, Prob is decimals are much slower (up to 20X times in some tests) than a double/float.

Decimals and Floats/Doubles cannot be compared without a cast whereas Floats and Doubles can. Decimals also allow the encoding or trailing zeros.



Java difference between int, float and double:

One Asked: Can you explain what makes this difference between float and double?

Sure. Imagine you had two decimal types, one with five significant digits, and one with ten.

What value would you use to represent pi for each of those types? In both cases you'd be trying to get as close to a number which you couldn't represent exactly - but you wouldn't end up with the same value, would you?

It's the same for float and double - both are binary floating point types, but double has more precision than float.

12 {int}
12.345678 {float}
12.345678910111213 {double}

float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the significant (or what follows from a scientific-notation number: 2.33728*1012; 33728 is the significand).
double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of significand.
By default, Java uses double to represent its floating-point numerals (so a literal 3.14 is typed double). It's also the data type that will give you a much larger number range, so I would strongly encourage its use over float.

Javascript:

12 {int}
12.345678 {float}
12.345678910111213 {double}

Actually All numbers in JavaScript are doubles: that is, they are stored as 64-bit IEEE-754 doubles.

That is, the goal is not to get a "double": the goal is to get the string reprsentation of a number formatted as "YYY.XX". For that, consider Number.toFixed, for instance:

(100).toFixed(2)
The result is the string (not a "double"!) "100.00". The parenthesis are required to avoid a grammar ambiguity in this case (it could also have been written as 100.0.toFixed or 100..toFixed), but would not be required if 100 was in a variable.

Happy coding!!


Tags:

Difference between decimal, float and double in programming

By Game Changer → Tuesday, April 25, 2017