implode 


string apg

JOIN ARRAY elements with a STRING.

This function is an alias to join.



 join 


string apg

JOIN ARRAY elements with a STRING.

This function is an alias to implode.



<?php

str implode 
arr $pieces )
or
str join arr $pieces )


where,

$pieces The ARRAY of STRINGS to implode

?>
 

$pieces


The ARRAY of STRINGS to implode.



<?php

str implode 
str $glue arr $pieces )
or
str join str $glue arr $pieces )


where,

$glue The defaults to an empty STRING

$pieces 
The ARRAY of STRINGS to implode

?>
 

$glue


Defaults to an empty STRING.



$pieces


The ARRAY of STRINGS to implode.





This function for historical reasons, can accept its parameters in either order.

For consistency with the function explode, however, it may be less confusing to use the documented order of arguments.

This function is binary-safe

A binary-safe function is one that treats its input as a raw stream of bytes and ignores every textual aspect it may have.

The term is mainly used in the PHP programming language to describe expected behaviour when passing binary data into functions whose main responsibility is text and string manipulating, and is used widely in the official PHP documentation.

From Wikipedia, the free encyclopedia.



  1 EXERCISE   

<?php

$pieces01 
= ["RED""GREEN""BLUE"];

$implstr01 implode($pieces01);

echo 
$implstr01;

?>

 RESULT   

REDGREENBLUE

No gluing character.


  2 EXERCISE   

<?php 

$pieces02 
= ['RED''GREEN','BLUE'];

$glue02 '+'

$inplstr02 implode($glue02$pieces02); 

echo 
$inplstr02

?>

 RESULT   

RED+GREEN+BLUE

Gluing with the + character..


  3 EXERCISE   

<?php 

$pieces03 
= ['RED' => 'ff0000'
                
'GREEN' => '00ff00'
                  
'BLUE' => '0000ff'];

$glue03 ' * '

$inplstr03 implode($glue03$pieces03);

echo 
$inplstr03

?>

 RESULT   

ff0000 * 00ff00 * 0000ff

Gluing with the ' * ' characters.


  4 EXERCISE   

<?php 

$pieces04 
= ['AEAD, Autenticated Encryption with 
Associated Data, is a form of encryption which 
simultaneously provides confidentiality, integrity, 
and authenticity assurances on the data.'
,'
GCM, Galois/Counter Mode is a mode of operation for 
symmetric key cryptographic block ciphers that has 
been widely adopted because of its efficiency 
and performance.'
,'
GCM throughput rates for state-of-the-art, high-speed 
communication channels can be achieved with 
reasonable hardware resources.'
'
CCM, (Counter with CBC-MAC) is a mode of operation for 
cryptographic block ciphers.'
'
It is an authenticated encryption algorithm designed 
to provide both authentication and confidentiality.'
'
CCM mode is only defined for block ciphers with a 
block length of 128 bits.'
];

$glue04 '<br><br>'

$inplstr04 implode($glue04$pieces04);

echo 
$inplstr04

?>

 RESULT   

AEAD, Autenticated Encryption with Associated Data, is a form of encryption which simultaneously provides confidentiality, integrity, and authenticity assurances on the data.

GCM, Galois/Counter Mode is a mode of operation for symmetric key cryptographic block ciphers that has been widely adopted because of its efficiency and performance.

GCM throughput rates for state-of-the-art, high-speed communication channels can be achieved with reasonable hardware resources.

CCM, (Counter with CBC-MAC) is a mode of operation for cryptographic block ciphers.

It is an authenticated encryption algorithm designed to provide both authentication and confidentiality.

CCM mode is only defined for block ciphers with a block length of 128 bits.


  5 EXERCISE   

<?php

$val01 
15*14;
$val02 15*16;
$val03 15*17;

$arr = [ 
    
"val15" => 15,
    
"val14" => 14,
    
"val16" => 16,
    
"val17" => 17 
         
];
    
    echo 
'<pre>';
    
print_r($arr);
    echo 
'</pre>';
    
    
    
$pieces implode('*'$arr);
    
    echo 
$pieces;
    
    
?>

  6 EXERCISE   

<?php

$s 
"het leven is net erwtensoep - 
je kunt er geen touw aan vastknopen"
;

for(
$i=0$i<3$i++) {
    echo 
implode(count_chars($s$i))."<br><br>";
}

echo 
$a count_chars($s3), "<br><br>";

echo (int) 
strlen(count_chars($s4)) == 256-strlen($a),"<br><br>";

echo 
strlen(count_chars($s4)) == 256-strlen($a),"<br>";

?>

  7 EXERCISE   

<?php

echo implode(array());
echo 
implode('nothing', array())."<br>";
echo 
implode(array('foo''bar''baz'))."<br>";
echo 
implode(':', array('foo''bar''baz'))."<br>";
echo 
implode(':', array('foo', array('bar''baz'), 'burp'));

?>

  8 EXERCISE   

<?php

echo "Testing implode() for basic operations.";
$arrays = array (
  array(
1,2),
  array(
1.1,2.2),
  array(array(
2),array(1)),
  array(
false,true),
  array(),
  array(
"a","aaaa","b","bbbb","c","ccccccccccccccccccccc")
);
/* loop to output string with ', ' as $glue, using implode() */
foreach ($arrays as $array) {
  
var_dumpimplode(', '$array) );
  
var_dump($array);
}

echo 
"<br><br>Testing implode() with variations of glue:<br>";
/* checking possible variations */
$pieces = array (
  
2,
  
0,
  -
639,
  
true,
  
"PHP",
  
false,
  
NULL,
  
"",
  
" ",
  
"string\x00with\x00...\0"
);
$glues = array (
  
"TRUE",
  
true,
  
false,
  array(
"key1""key2"),
  
"",
  
" ",
  
"string\x00between",
  
NULL,
  -
0,
  
'\0'
);
/* loop through to display a string 
      containing all the array $pieces 
      in the same order,
      with the $glue string between each element  */
$counter 1;
foreach(
$glues as $glue) {
  echo 
"<br><br>Iteration: $counter<br>";
  try {
       
var_dump(implode($glue$pieces));
  } catch (
TypeError $exception) {
      echo 
$exception->getMessage() . "<br>";
  }
  
$counter++;
}

/* empty string */
echo "<br><br>Testing implode() on empty string:<br>";
try {
    
implode("");
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

/* checking sub-arrays */
echo "<br><br>Testing implode() on sub-arrays:<br>";
$sub_array = array(array(1,2,3,4), 
                        array(
=> "one"=> "two"), "PHP"50);
var_dump(implode("TEST"$sub_array));
try {
   
var_dump(implode(array(1234), $sub_array));
} catch (
TypeError $exception) {
  echo 
$exception->getMessage() . "<br>";
}
try {
   
var_dumpimplode(2$sub_array) );
} catch (
TypeError $exception) {
  echo 
$exception->getMessage() . "<br>";
}

echo 
"<br><br>Testing implode() on objects:<br>";
/* checking on objects */
class foo
{
  function 
__toString() {
    return 
"Object";
  }
}

$obj = new foo(); //creating new object
$arr = array();
$arr[0] = &$obj;
$arr[1] = &$obj;
var_dumpimplode(","$arr) );
var_dump($arr);

/* Checking on resource types */
echo "<br><br>Testing end() on resource type:<br>";
/* file type resource */
$file_handle fopen(__FILE__"r");

/* directory type resource */
$dir_handle opendir__DIR__ );

/* store resources in array for comparison */
$resources = array($file_handle$dir_handle);

var_dumpimplode("::"$resources) );

echo 
"<br><br>Testing error conditions:<br>";

/* only glue */
try {
    
var_dumpimplode("glue") );
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

/* int as pieces */
try {
    
var_dumpimplode("glue",1234) );
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

/* NULL as pieces */
try {
    
var_dumpimplode("glue"NULL) );
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

/* pieces as NULL array */
try {
    
var_dumpimplode(",", array(NULL)) );
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

/* integer as glue */
try {
    
var_dumpimplode(12"pieces") );
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

/* NULL as glue */
try {
    
var_dumpimplode(NULL"abcd") );
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

/* closing resource handles */
fclose($file_handle);
closedir($dir_handle);

?>