PHP Compact creates an array _php instance with variables

Source: Internet
Author: User
Tags compact mixed

PHP Compact creates an array from a variable

The compact function creates an array of one or more variables, and the array includes the variable names and their values.

The function behaves exactly the opposite of extract ().

Basic syntax

Array compact (mixed $varname [, mixed $ ...])

Creates an array that contains the variables and their values.

For each parameter, the compact () looks for the variable name in the current symbol table and adds it to the output array, and the variable name becomes the key and the contents of the variable becomes the value of the key. Simply put, it does the exact opposite of extract (). Returns the array after which all the variables are added.

Any string that does not have a variable name is skipped.

Parameter introduction:

Parameters Description
VarName Necessary. Can be a string with a variable name, or an array of variables.
... Optional. Can be a string with a variable name, or an array of variables. Multiple parameters are allowed.
return value

Returns an array with all the variable names and their values.

Attention:

    1. Any string that does not have a variable name is skipped.
    2. If an array exists in the argument, the value of the variable in the array is also fetched.
    3. Because a variable variable may not be used inside a function for a super global array of PHP, you cannot pass a super global array into the compact ().

Instance:

<?php
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location _vars = Array (
  "City",
  "state"
);
$result = Compact ("event", "Nothing_here", "Location_vars");
Print_r ($result);
echo "<br/><br/>";
$result = Compact ("event", "Nothing_here", $location _vars);
Print_r ($result);

Run Result:

Array ([Event] => SIGGRAPH [location_vars] => Array ([0] => City [1] => State)

Array ([Event] => SIGGRAPH [city] => San Francisco [State] => CA)

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.