Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2004, 06:11 AM
Dan_UPC Dan_UPC is offline
 
Join Date: Dec 2002
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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
Reply With Quote
  #2  
Old 11-03-2004, 09:50 AM
Colin F's Avatar
Colin F Colin F is offline
 
Join Date: Jul 2004
Location: Switzerland
Posts: 1,551
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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)
Reply With Quote
  #3  
Old 11-03-2004, 10:23 AM
Dan_UPC Dan_UPC is offline
 
Join Date: Dec 2002
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #4  
Old 11-03-2004, 02:19 PM
Dan_UPC Dan_UPC is offline
 
Join Date: Dec 2002
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #5  
Old 11-03-2004, 03:49 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

actually == should return a boolean value, ie true or false and not 1 or 0.
Reply With Quote
  #6  
Old 11-03-2004, 03:52 PM
Dan_UPC Dan_UPC is offline
 
Join Date: Dec 2002
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #7  
Old 11-03-2004, 04:05 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #8  
Old 11-03-2004, 04:59 PM
Dan_UPC Dan_UPC is offline
 
Join Date: Dec 2002
Posts: 49
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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)?
Reply With Quote
  #9  
Old 11-03-2004, 05:04 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #10  
Old 11-03-2004, 05:21 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

just as i expected..
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:38 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.03917 seconds
  • Memory Usage 2,250KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete