Jump to content

php concatenate


Go to solution Solved by ManiacDan,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/290524-php-concatenate/
Share on other sites

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>';
}
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488202
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488261
Share on other sites

  • Solution

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 > 
Link to comment
https://forums.phpfreaks.com/topic/290524-php-concatenate/#findComment-1488307
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.