By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,930 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,930 IT Pros & Developers. It's quick & easy.

add up an integer with the result from substr()

P: n/a
Hi,

I'm trying to add up an integer with the result from substr() like

$second_int = substr( .... ) + $int;

I know the result from substr() is an integer and so is $int but in the end
$second_int only has the result from the substr() and hasn't add up the
$int. What am I doing wrong in here... tried quite a few things but well....

Thanks in advance, Maarten
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
MuffinMan wrote:
I'm trying to add up an integer with the result from substr() like

$second_int = substr( .... ) + $int;
Lookss good.
I know the result from substr() is an integer and so is $int but in the end
$second_int only has the result from the substr() and hasn't add up the
$int. What am I doing wrong in here...
No idea ...
tried quite a few things but well....


why don't you post what you tried?


$ cat xx.php
<?php
$int = 17;
$text = '01234';

$y = array();
$y[] = substr($text, 1, 2) + $int;
$y[] = substr($text, 1) + $int;
$y[] = substr($text, 1, 55) + $int;
$y[] = substr($text, 1, -2) + $int;
$y[] = substr($text, 1, -22) + $int;
$y[] = substr($text, -3, 2) + $int;
$y[] = substr($text, -3) + $int;
$y[] = substr($text, -3, 55) + $int;
$y[] = substr($text, -3, -2) + $int;
$y[] = substr($text, -3, -22) + $int;

print_r($y);
?>

$ php xx.php
Array
(
[0] => 29
[1] => 1251
[2] => 1251
[3] => 29
[4] => 17
[5] => 40
[6] => 251
[7] => 251
[8] => 19
[9] => 17
)

$
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
> $second_int = substr( .... ) + $int;

$second_int = intval(substr( .... )) + $int;
should work imho.
http://fr2.php.net/manual/en/function.intval.php
Jul 17 '05 #3

P: n/a
| why don't you post what you tried?
Well, you're right nothing wrong in it but vars were defined outside a
function which gave a problem... next problem.... next thread..

Maarten
Jul 17 '05 #4

P: n/a
MuffinMan wrote:
Well, you're right nothing wrong in it but vars were defined outside a
function which gave a problem... next problem.... next thread..


You didn't make them global now, did you? :-)

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #5

P: n/a

| You didn't make them global now, did you? :-)

I didn't make them global tried to but failed.... Now it's ok.

Maarten
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.