How to copy a string in PHP

7 Answers

0 votes
// Copy using simple assignment 

$src = "Programming is fun";

$dest = $src;

echo $dest;


/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using strval() 

$src = "Programming is fun";

$dest = strval($src);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using sprintf() 

$src = "Programming is fun";

$dest = sprintf("%s", $src);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using implode() 

$src = "Programming is fun";

$chars = str_split($src);

$dest = implode("", $chars);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using str_repeat() trick 

$src = "Programming is fun";

$dest = str_repeat($src, 1);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using substr() 

$src = "Programming is fun";

$dest = substr($src, 0);

echo $dest;




/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz
0 votes
// Copy using manual loop 

$src = "Programming is fun";
$dest = "";

for ($i = 0; $i < strlen($src); $i++) {
    $dest .= $src[$i];
}

echo $dest;




/*
run:

Programming is fun

*/

 



answered 2 days ago by avibootz

Related questions

6 answers 19 views
7 answers 19 views
8 answers 27 views
8 answers 24 views
7 answers 19 views
...