Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a slash do to a string with a dot in php?

Tags:

php

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

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

output 13

When

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

outputs 13.pressrum

like image 271
Himmators Avatar asked Jan 27 '26 02:01

Himmators


2 Answers

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
like image 104
Hecke29 Avatar answered Jan 29 '26 16:01

Hecke29


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

like image 34
Reeno Avatar answered Jan 29 '26 14:01

Reeno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!