PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Operadores de Matrices> <Operadores de Lógica
Last updated: Fri, 22 Aug 2008

view this page in

Operadores de Cadena

Existen dos operadores para datos tipo string. El primero es el operador de concatenación ('.'), el cual devuelve el resultado de concatenar sus argumentas a lado derecho e izquierdo. El segundo es el operador de asignación sobre concatenación ('.='), el cual adiciona el argumento del lado derecho al argumento en el lado izquierdo. Por favor consulte Operadores de Asignación para más información.

<?php
$a 
"¡Hola ";
$b $a "Mundo!"// ahora $b contiene "¡Hola Mundo!"

$a "¡Hola ";
$a .= "Mundo!";     // ahora $a contiene "¡Hola Mundo!"
?>

Vea también las secciones del manual sobre el tipo String y las funciones de Cadenas.



Operadores de Matrices> <Operadores de Lógica
Last updated: Fri, 22 Aug 2008
 
add a note add a note User Contributed Notes
Operadores de Cadena
mariusads::at::helpedia.com
27-Aug-2008 11:44
Be careful so that you don't type "." instead of ";" at the end of a line.

It took me more than 30 minutes to debug a long script because of something like this:

<?
echo 'a'.
$c = 'x';
echo 'b';
echo 'c';
?>

The output is "axbc", because of the dot on the first line.
mehea
20-May-2008 02:31
I thought string operators were for use with strings or strings and numbers.  But that is incorrect.  You can use the '.' operator to concatenate two numbers, as follows:

echo 1 . 2;

I assume that behind the scenes the 1 and 2 are converted to strings to allow the concatenation.  What triggers the conversion? I'll guess the dot operator.
kevin at metalaxe dot com
10-Nov-2006 03:57
I ran the follow script and found that using "$var" was 'mostly' slower than using ' '.$var

<?php
$var
= 1;

for(
$x=0; $x < 101; $x++ )
{
    echo
'<br /><br />var = int( '.$var.' )<br />';
   
   
$timer->reset();
    for(
$i=0; $i<100001; $i++ )
    {
       
$string = " {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var}";
        unset(
$string );
    }
    echo
'One string with 15 $vars was set using one concat 100000 times and took '.$timer->fetch_time().' seconds to execute <br />';
   
   
$timer->reset();
    for(
$i=0; $i<100001; $i++ )
    {
       
$string = ' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var;
        unset(
$string );
    }
    echo
'One string with 15 instances of $var was set using multiple concats 100000 times and took '.$timer->fetch_time().' seconds to execute';
}
exit();
?>

Replacing $timer with a generic timing class of course.
caliban at darklock dot com
29-Mar-2006 09:10
WRT Stephen's note:

My example of concatenation and array methods of string building does not include the interstitial logic, which is expected to include conditionals.

Concatenation method:

$str="This is my list";
if($list=="o") $str.="<ol>";
else $str.="<ul>";
foreach($item as $i) $str.="<li>$i</li>";
if($list=="o") $str.="</ol>";
else $str.="</ul>";

Array method:

$str=array("This is my list");
if($list=="o") $str[]="<ol>";
else $str[]="<ul>";
foreach($item as $i) $str[]="<li>$i</li>";
if($list=="o") $str[]="</ol>";
else $str[]="</ul>";
$str=implode("",$str);

You can't do either of these with a single double-quoted string. However, if what you are doing CAN be done in a single double-quoted string, Stephen is completely correct in observing that you should do that instead of concatenating.
Stephen Clay
23-Dec-2005 04:10
<?php
"{$str1}{$str2}{$str3}"; // one concat = fast
 
$str1. $str2. $str3;   // two concats = slow
?>
Use double quotes to concat more than two strings instead of multiple '.' operators.  PHP is forced to re-concatenate with every '.' operator.
caliban at darklock dot com
15-Dec-2004 04:57
String concatenation is faster than the array method:

$str="";
$str.="Some string";
$str.="Some other string";
...
$str.="The last string";

That runs roughly twice as fast as:

$str=array();
$str[]="Some string";
$str[]="Some other string";
...
$str[]="The last string";
$str=implode("",$str);

Not that I think this is a terribly widespread practice, but I've got an awful lot of legacy code with this array method in it and a comment to the effect that it's faster than string concatenation. Testing has shown the exact opposite, so I figured I'd enlighten anyone else with this misconception.
anders dot benke at telia dot com
27-Apr-2004 06:53
A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.

Example:

<php
$var = 3;

echo "Result: " . $var + 3;
?>

The above will print out "3" instead of "Result: 6", since first the string "Result3" is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

To print "Result: 6", use parantheses to alter precedence:

<php
$var = 3;

echo "Result: " . ($var + 3);
?>

Operadores de Matrices> <Operadores de Lógica
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites