Max Frai
Max Frai

Reputation: 64276

Php and strings

I'm receiving string variable in php and need to add something to it.

I dump the variable and see:

var_dump($myvar);

string(length) " ... "

After that I write:

$myvar += '~';

And it's inserted into DB (This is wordpress plugin and I'm adding a text into post content).

As a result, I get '0' :( What could it be?

Upvotes: 1

Views: 107

Answers (5)

Spudley
Spudley

Reputation: 168685

PHP uses a dot as the operator to concatenate strings, not a plus sign.

The plus sign adds numerically. This explains why you are getting the result of zero, because both of the strings you are adding are numerically equal to zero.

Both plus and dot can be combined with the equal sign in the way you are doing, so the corrected version of your line of code would look like this:

$myvar .= '~';

Hope that helps.

Upvotes: 3

John Smith
John Smith

Reputation: 808

If you want to add "~" behind your string you should do this:

REPLACE

$myvar += '~';

BY

$myvar .= '~';

Upvotes: 3

K6t
K6t

Reputation: 1845

instead of '+' used in js , '.' in PHP

$myvar .= '~';

Upvotes: 1

Ameer
Ameer

Reputation: 771

you can just use $myvar .= "~" it will add '~' to the $myvar.

Upvotes: 1

user784540
user784540

Reputation:

Try this code to add ~ symbol to your variable:

$myvar = $myvar."~"

Upvotes: 1

Related Questions