raytaylornz Posted October 25, 2009 Share Posted October 25, 2009 I have the following code and am trying to add the strings together to make a total. The monthup and monthdown strings are extracted from a text file earlier in the script. echo "$monthup <br>"; echo "$monthdown <br>"; $answer = $monthup + $monthdown; echo $answer; It outputs 36809697 565151504 0 The last line should be the other 2 added together which equals 601961201 Anyone got any ideas? Thanks in advance for any help you can give. As you can see I am new to php. Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/ Share on other sites More sharing options...
trq Posted October 25, 2009 Share Posted October 25, 2009 Strings are strings, not numbers. What exactly do you expect? Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-943814 Share on other sites More sharing options...
raytaylornz Posted October 25, 2009 Author Share Posted October 25, 2009 Ok. I was hoping to add them together. How would I do it then? Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-943945 Share on other sites More sharing options...
Mchl Posted October 25, 2009 Share Posted October 25, 2009 [edited] Tottally wrong answer was here Try $answer = (int)$monthup + (int)$monthdown; Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-943951 Share on other sites More sharing options...
salathe Posted October 25, 2009 Share Posted October 25, 2009 Out of interest can you tell us what var_dump($monthup, $monthdown) outputs? Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-943967 Share on other sites More sharing options...
Kloplop321 Posted October 29, 2009 Share Posted October 29, 2009 you could do echo "$monthup <br>"; echo "$monthdown <br>"; $answer = (int)$monthup + (int)$monthdown; echo $answer; or $monthup = (int)trim($monthup); $monthdown = (int)trim($monthdown); echo "$monthup <br>"; echo "$monthdown <br>"; $answer = (int)$monthup + (int)$monthdown; echo $answer; You may need to cast them to numbers as an integer or a real number[ use (real) instead of (int)] before adding them together. if the variable has a space before the number by accident, trim() will remove it. Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946765 Share on other sites More sharing options...
Daniel0 Posted October 29, 2009 Share Posted October 29, 2009 Or you know, he could just not add the HTML stuff right before he is going to treat it as a numeric value... Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946873 Share on other sites More sharing options...
Mchl Posted October 29, 2009 Share Posted October 29, 2009 He's just echoing it, not adding anything... something's strange here... Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946889 Share on other sites More sharing options...
nadeemshafi9 Posted October 29, 2009 Share Posted October 29, 2009 strings are just arrays arnt they Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946899 Share on other sites More sharing options...
Daniel0 Posted October 29, 2009 Share Posted October 29, 2009 strings are just arrays arnt they In C, strings are arrays of chars, but that's not the case in PHP. Actually, there isn't even a char data type in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946906 Share on other sites More sharing options...
nadeemshafi9 Posted October 29, 2009 Share Posted October 29, 2009 so what your saying that the basic object string is a less complex object in php than in c Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946908 Share on other sites More sharing options...
Daniel0 Posted October 29, 2009 Share Posted October 29, 2009 I don't know what you mean with "complex", but I would say it's more complex given that it has more features and you can dynamically change the size of it without having to reallocate memory manually. Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946910 Share on other sites More sharing options...
nadeemshafi9 Posted October 29, 2009 Share Posted October 29, 2009 that makes sense in the c is simpler and closer lower level to the machine Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946912 Share on other sites More sharing options...
Mchl Posted October 29, 2009 Share Posted October 29, 2009 PHP strings can be accessed with array operator <?php $f = "PHP Freaks"; for ($i = 0; $i < strlen($f); $i++) echo $f[$i]; That's where confusion may be coming from. However, you can't do this with foreach and is_array($f); will return false. Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946920 Share on other sites More sharing options...
nadeemshafi9 Posted October 29, 2009 Share Posted October 29, 2009 i suppose it comes down to the strength of the type in the environment, i suppose if you start adding more functionality to types the line boundaries start to disappear Quote Link to comment https://forums.phpfreaks.com/topic/178890-add-two-strings-together-0/#findComment-946921 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.