Jump to: navigation, search

Php compact

From w3cyberlearnings

Contents

PHP function compact

This function creates array from variable and its value.

Syntax compact

  • v1: variable 1
  • v2: variable 2
compact(v1, v2,...);

Return

  • When the variable is not existed, the variable names will be skiped.

Example 1

<?php

$ella = 30;
$amelia = 2;
$liam = 21;

$user_score = compact("ella", "amelia", "liam");

print_r($user_score);
?>

Output


Array
(
    [ella] => 30
    [amelia] => 2
    [liam] => 21
)

Example 2

<?php

$ella = 30;
$amelia = 2;
$liam = 21;

$bob = 40;
$johy = 41;

$name_s = array('bob', 'johy');
$user_score = compact($name_s, "ella", "amelia", "liam");

print_r($user_score);
?>

Output


Array
(
    [bob] => 40
    [johy] => 41
    [ella] => 30
    [amelia] => 2
    [liam] => 21
)

Related Links


Navigation
Web
SQL
MISC
References