vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Weird PHP variable behaviour (https://vborg.vbsupport.ru/showthread.php?t=71302)

Dan_UPC 11-03-2004 06:11 AM

Weird PHP variable behaviour
 
Here's the problem.

PHP Code:

$val=123;
echo 
$val==intval($val); // Echo: 1

$val=123.123;
echo 
$val==intval($val); // Echo: 

I seem to remember that the 2nd echo used to output 0 as expected. This changed recently but I'm not sure exactly why. Perhaps it was when we upgraded to PHP5? Is there something I can set in php.ini so that I'll get the correct output again? This problem is breaking a bunch of vB code and old custom scripts so I'd like to return PHP to it's former behaviour as quick as possible.

Thanks.

- Dan

Colin F 11-03-2004 09:50 AM

might it be that it has something to do with the regional settings, meaning that he used to interpret the dot (.) as a character, making it not a valid integer, but now interprets it as the kind of dot between the number and the decimal digits (which would mean it's a valid integer)

Dan_UPC 11-03-2004 10:23 AM

No that's not it. The region hasn't changed.

123.123 does not equal intval(123.123) because intval(123.123) equals 123 so it's doing that much correctly. What it's not doing is Echoing the 0.

Dan_UPC 11-03-2004 02:19 PM

Another example of broken code from this behaviour is the vB 2.2.x update threads routine in /admin/misc.php if there are no attachments for a particular thread,
$attachsum=
instead of
$attachsum=0

Xenon 11-03-2004 03:49 PM

actually == should return a boolean value, ie true or false and not 1 or 0.

Dan_UPC 11-03-2004 03:52 PM

Thanks Xenon.

That's fair enough but if you try it yourself, you'll see that you get an output of 1 when it's true.

What about the 2nd example?

Xenon 11-03-2004 04:05 PM

Hmm, very wierd, maybe they choosed a behaviour like that to make it easier in comparisons.

so 1 will be true in every condition, and false will have an empty string as output, so it will always result in a false.

actually to get the 0-1 way, just use intval($val==intval($val)); that should return what you want.

edit: vb2.2 isn't compatible to the newest default settings of php, as php doesn't interpolate that much as php4 does.
so you need a better coding to run things in php5. Some settings can be set so it acts like php4 in a lot of ways, but not on every aspect i think.

Dan_UPC 11-03-2004 04:59 PM

Quote:

Originally Posted by Xenon
edit: vb2.2 isn't compatible to the newest default settings of php, as php doesn't interpolate that much as php4 does.
so you need a better coding to run things in php5. Some settings can be set so it acts like php4 in a lot of ways, but not on every aspect i think.

This is what I expected was the problem. Does anyone know how I can get this php 3 / 4 behaviour to be the default in php 5 (I hope there is a php.ini setting? as I really don't wan't to have to repair all the old code)?

filburt1 11-03-2004 05:04 PM

Quote:

Originally Posted by Dan_UPC
Here's the problem.

PHP Code:

$val=123;
echo 
$val==intval($val); // Echo: 1

$val=123.123;
echo 
$val==intval($val); // Echo: 

I seem to remember that the 2nd echo used to output 0 as expected. This changed recently but I'm not sure exactly why. Perhaps it was when we upgraded to PHP5? Is there something I can set in php.ini so that I'll get the correct output again? This problem is breaking a bunch of vB code and old custom scripts so I'd like to return PHP to it's former behaviour as quick as possible.

Thanks.

- Dan

False has always echoed an empty string for me.

Quote:

A boolean TRUE value is converted to the string "1", the FALSE value is represented as "" (empty string). This way you can convert back and forth between boolean and string values.

Xenon 11-03-2004 05:21 PM

just as i expected..


All times are GMT. The time now is 11:04 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01899 seconds
  • Memory Usage 1,736KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete