470,855 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

add up an integer with the result from substr()

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
5 2923
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
> $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
| 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
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

| 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.

Similar topics

6 posts views Thread by Andrew | last post: by
3 posts views Thread by news.hku.hk | last post: by
2 posts views Thread by lawpoop | last post: by
2 posts views Thread by lizayica | last post: by
25 posts views Thread by Tom | last post: by
11 posts views Thread by panther | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.