Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
neralex
Site Admin



Joined: Aug 22, 2007
Posts: 1772

PostPosted: Fri Apr 19, 2013 12:19 pm Reply with quote

I have question for better understanding.

In some modules of RN25x i have seen the type-casting from a string to a integer inside the switch($op) cases and in other modules it was typed in the function self.

Code:
switch ($op) {

   default:
   theindex(intval($new_topic));
   break;
}


or inside a function:

Code:
(int)$topic


Where is here the difference or what is the correct using?

_________________
Github: RavenNuke 
View user's profile Send private message
spasticdonkey
RavenNuke(tm) Development Team



Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA

PostPosted: Sat Apr 20, 2013 9:48 pm Reply with quote

From my understanding, in terms of validating a $var that is an integer, they both do about the same thing. They can differ somewhat in how they handle values that are not integers. I usually use intval but not for any particular reason. someone else may have a better explanation... Smile
 
View user's profile Send private message Visit poster's website
wHiTeHaT
Life Cycles Becoming CPU Cycles



Joined: Jul 18, 2004
Posts: 579

PostPosted: Sun Apr 21, 2013 2:33 pm Reply with quote

[ Only registered users can see links on this board! Get registered or login! ]

And:
[ Only registered users can see links on this board! Get registered or login! ]
 
View user's profile Send private message Send e-mail
neralex







PostPosted: Sun Apr 21, 2013 3:06 pm Reply with quote

Thanks!
 
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Mon Apr 29, 2013 10:57 am Reply with quote

In the context of Ravennuke, the excessive use of int and/or intval has always driven me a little ways up the wall. There are many cases such as $x=int($topic) where we are processing a variable that should be derived from the values in an integer field in the database. If a value in $topic is being passed in that is not an integer then some sort of error message should be generated. If the value of $topic is a string we are basically converting it to 0 (zero) with current logic and then going merrily along our way. It shouldn't work that way. You can't get a non-integer value out of an integer field and you shouldn't process it as the value 0.

Unfortunately we don't have any graceful error processing that would also provide manageable information to the site admin in RN.
 
View user's profile Send private message Visit poster's website
neralex







PostPosted: Mon Apr 29, 2013 11:36 am Reply with quote

Ok thanks. It seemed to me also a bit strange. From the logical point i'm totally with you. I saw this casting was used befor the value passed in a sql-query to ensure that the value is numeric. I personally use in this case a check with is_numeric($value) before i start the query.
 
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Thu May 02, 2013 4:49 pm Reply with quote

Be careful as is_numeric() doesn't mean integer. It just means it is a number. When it comes to cleansing integer fields from outside (external) influences, either are fine but one does have to be careful, as fkelly has said, about changing the functional meaning. If having a string convert into '0' doesn't cause a functional issue, then what is the harm...

Complete agree that re-casting an integer field pulled straight from a DB call is just wasteful (was going to say "insane"). I think the same about saving encoded data to the database... Smile

There is still a ton of legacy code even in RN2.51 today. We chip away at it as we open up modules and do major surgery. Call it "Progressive Correction" if you like.

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©