Jump to content

Adding to a string or number


Drezard

Recommended Posts

So basically I have these numbers:

 


$number1 = 5;

$number2 = 25;

$number3 = 50;

 

And I want to add them on to the end of each other. So I tried to use code like this:

 

$numbers = $number1 . $number2 . $number3;

 

But that didn't work all it outputs is the last number. What other code could I use?

 

Regards, Daniel

 

 

Link to comment
https://forums.phpfreaks.com/topic/53955-adding-to-a-string-or-number/
Share on other sites

I don't understand what you mean. I'll try to figure it out though. Here's what I've got:

 

<?php
$number1 = 5;
$number2 = 25;
$number3 = 50;
$numbers = $number1 . $number2 . $number3;

print $numbers;
?>

 

Or do you mean to add them like:

$number1 + $number2 + number3 = 80

 

Again, I don't completely understand what you mean so... yeah.

Using this should work:

echo $number1 . $number2 . $number3;

 

Having to concat an empty string onto a number is waste of processing. Remove the "" and use just the concatenation operator.

<?php

$number1 = 5;
$number2 = 21;
$number3 = 88;

$numbers = $number1 . $number2 . $number3;

echo $numbers;

?>

Output is 52188

 

Can't see how it doesn't work for you. How areyou echoing $numbers?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.