et4891 Posted August 18, 2014 Share Posted August 18, 2014 I know that usually when we concatenante in PHP we use the period . to concatenate but I have been watching a few videos and they seems to use a comma , instead of . I didn't know about the comma before and if I didn't hear it wrong the video says both would work just depend on the way you work. So I tried using comma for testing such as echo '<pre>' , print_r($var) , '</pre>'; which works fine but when I use it in a function for fun such as function dd($var){ return '<pre>' , print_r($var) , '</pre>'; } this gives me errors about the comma but if I use period as concatenate the page would not return as html tag <pre> I know that I can just use var_dump and I tried searching things like difference between , and . in php or something similiar I couldn't find a page to explain the major difference. Can someone give me a hand? Sorry if this question is too stupid though. Quote Link to comment https://forums.phpfreaks.com/topic/290524-php-concatenate/ Share on other sites More sharing options...
CroNiX Posted August 18, 2014 Share Posted August 18, 2014 (edited) because print_r sends to the output and doesn't return a string unless you set the 2nd parameter to TRUE. http://php.net/manual/en/function.print-r.php Edited August 18, 2014 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488201 Share on other sites More sharing options...
Ch0cu3r Posted August 18, 2014 Share Posted August 18, 2014 Yes you can concatenate strings using comma or period but it depends on the context it is used. Such as you can only concatenate using commas in print and echo constructs. In any other case you must use periods. Personally I just stick to periods. the page would not return as html tag <pre> You mean to say the value returned from print_r is not contained within the <pre></pre> tags? By default print_r will just print the contents of value given. If you need capture the value then you need to tell it to return its value. As CroNix said above. function dd($var){ return '<pre>' . print_r($var, 1) . '</pre>'; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488202 Share on other sites More sharing options...
trq Posted August 19, 2014 Share Posted August 19, 2014 PHP's only concatenation operator is a period, end of story. What you are mistaking for concatenation with echo and print are actually arguments. Function (or in this case language construct) arguments are separated by comma. Because echo and print are "language constructs" they do not need () brackets around their arguments either. If you look at the man page for echo, you see that it simply "Outputs all parameters". Hence, it kind of acts/looks like concatenation, but its not. Quote Link to comment https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488261 Share on other sites More sharing options...
Solution ManiacDan Posted August 19, 2014 Solution Share Posted August 19, 2014 To clarify a bit: print() accepts arguments without parentheses, but does not accept multiple arguments. The function definition for print is: int print ( string $arg ) The function definition for echo is: void echo ( string $arg1 [, string $... ] ) Print and echo are both language constructs, but they perform differently. Echo can have parentheses (but only if you're using one argument), whereas print has optional parentheses (it works with or without them). Echo can accept comma-delimited arguments whereas print cannot. Both functions accept concatenated objects, but for (negligible) speed reasons you should use commas when using echo. See my cli PHP example:php > $a = "Hello, "; php > $b = "World!\n"; php > php > echo($a); Hello, php > echo($a,$b); PHP Parse error: syntax error, unexpected ',' in php shell code on line 1 php > echo $a, $b; Hello, World! php > php > print $a; Hello, php > print $a, $b; PHP Parse error: syntax error, unexpected ',' in php shell code on line 1 php > print( $a ); Hello, php > print( $a, $b ); PHP Parse error: syntax error, unexpected ',' in php shell code on line 1 php > php > php > echo $a . $b; Hello, World! php > echo( $a . $b); Hello, World! php > print $a . $b; Hello, World! php > print( $a . $b ); Hello, World! php > Quote Link to comment https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488307 Share on other sites More sharing options...
et4891 Posted August 21, 2014 Author Share Posted August 21, 2014 Thanks a lot everyone~!~! Quote Link to comment https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488471 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.