note 52883 added to function.settype

From: Date: Mon, 16 May 2005 19:22:12 +0000
Subject: note 52883 added to function.settype
Groups: php.notes 
Request: Send a blank email to php-notes+get-89504@lists.php.net to get a copy of this message
I needed to pull a zerofilled integer out of a MySQL table and increment it by a certain amount. Unfortunately, PHP treated this integer as if it were a string when I tried to add an amount to it. For instance: <?php $a = '0100'; $b = $a + 1; print $b; // This would print 64 instead of 101. ?> To fix this I created a simple function: <?php function convertToInt($string) { $y = ltrim($string, '0'); $z = 0 + $y; return $z; } // Now I can add any integer to my converted integer $a = '0100'; $b = 2 + convertToInt($a); print $b; // This prints 102 ?> ---- Manual Page -- http://www.php.net/manual/en/function.settype.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52883 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52883&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52883&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52883&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52883&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52883&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52883&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89504) next »