0

I found the following code in http://in3.php.net/manual/en/function.chr.php

<?php 
function randPass($len) 
{ 
 $pw = ''; //intialize to be blank 
 for($i=0;$i<$len;$i++) 
 { 
   switch(rand(1,3)) 
   { 
     case 1: $pw.=chr(rand(48,57));  break; //0-9 
     case 2: $pw.=chr(rand(65,90));  break; //A-Z 
     case 3: $pw.=chr(rand(97,122)); break; //a-z 
   } 
 } 
 return $pw; 
} 
?> 

Example: 

<?php 
 $password = randPass(10); //assigns 10-character password 
?> 

Could some kindly explain me the use or effect of a . after $pw .I tried looking for similar question,but could not find one.If there is any related question,plz provide link.

Thanks

Mayur
  • 291
  • 1
  • 3
  • 16

5 Answers5

1

. is string concatenation.

$a = 'hello';
$b = 'there';

then

echo $a . $b;

prints

hellothere
mzedeler
  • 4,177
  • 4
  • 28
  • 41
0

Concatenation Operator.

$a = "Hello ";
$a .= "World!"; 
// $a = Hello World!

$a = "Hello ";
$b = "there ";
$c = "stack ";
$d = "overflow ";
$a .= $b;
$a .= $c;
$a .= $d;
echo $a;

// $a = Hello there stack overflow
brbcoding
  • 13,378
  • 2
  • 37
  • 51
  • Why the downvote/no comment? This is definitely correct. – brbcoding May 02 '13 at 18:29
  • 1
    Indeed. Not only it was the 1st answer, it's also correct. Maybe you should flag it (if the downvote comes from another answer author, this is unfair) – Déjà vu May 02 '13 at 18:33
0

It use for Concatenation.
When you use it like this .= it concatenates the values.

$str = '';
$str .= 'My ';
$str .= 'Name ';
$str .= 'Is ';
$str .= 'Siamak.';
echo $str;

Output is : My Name Is Siamak.
In the other cases, for example in a loop:

$str = '';
$i=0;
while($i<10)
{
   $str .= $i;
   $i++;
}
echo $str;

And output is: 123456789

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
0
$a = 'hello';
$a .= ' world';

Is the same as:

$a = 'hello';
$a = $a . ' world';
Marcos
  • 1,240
  • 10
  • 20
0
    $a = "Hi!";

    $a .= " I";
    $a .= " am";
    $a .= " new";
    $a .= " to";
    $a .= " PHP & StackOverflow";

echo $a;


//echos Hi! I am new to PHP & StackOverflow

.= simply appends

Mayur
  • 291
  • 1
  • 3
  • 16