2

If $GLOBALS['filefolder'] = 13.pressrum How come

$filefolder = '/'+$GLOBALS['filefolder'];
echo $filefolder

output 13

When

$filefolder = $GLOBALS['filefolder'];
echo $filfolder

outputs 13.pressrum

2

4 Answers 4

7

The operator + is numeric / mathematical in PHP. So '/'+$GLOBALS['filefolder']; is equal to 0+13 in your case (php casts both to integer) which actually is 13.

To concatenate use .

$filefolder = '/' . $GLOBALS['filefolder'];
echo $filefolder
4

A + adds two numbers as in 1+1. So if you use it to concatenate two strings, they get converted to integers internally and thus the result is 13. A dot (.) concatenates two string. So you have to write $filefolder = '/'.$GLOBALS['filefolder'];

You probably confused it with JavaScript, where + is used to concatenate strings (and also for adding numbers, but that's another topic...).

2

The + operator in php is the sum. If you sum '/' + "13.pressrum", both will be casted to integer.

You should use . to concatenate strings.

2

+ is a mathematical operator, so PHP tries to find numbers in / and 13.pressrum it only finds the 13 so you get the result of 0 + 13, which is 13.

I guess what you wanted to do is concatenate the strings, try this:

$filefolder = '/'.$GLOBALS['filefolder'];
echo $filefolder

It should output /13.pressrum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.