PHP » PYTHON |
login |
register |
about
|
array = ('foo', 'bar', 'foobar') # Assigns all values. (foo, bar, foobar) = array #Prints 'foo - bar - foobar' print '%s - %s - %s' % (foo, bar, foobar) For single assignments, the tuple must have an ending comma, otherwise a ValueError is raised. In Python, unlike PHP's list(), you cannot just skip an assignment with a space or by ignoring the other items. You also may not assign one variable and leave all other assignments blank. A simple solution is to just use an underscore: array = ('foo', 'bar', 'foobar') # Assigning some values (foo, _, _) = array # Skipping (_,_,foo2) = array # Prints 'foo - foobar' print '%s - %s' % (foo, foo2) list(PHP 4, PHP 5) list — Assign variables as if they were an array DescriptionLike array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation. Parameters
Return ValuesNo value is returned. Examples
Example #1 list() examples
<?php
Example #2 An example use of list()
<table>
Example #3 Using nested list()
<?php int(1) int(2) int(3)
Example #4 Using list() with array indices
<?php Gives the following output (note the order of the elements compared in which order they were written in the list() syntax): array(3) { [2]=> string(8) "caffeine" [1]=> string(5) "brown" [0]=> string(6) "coffee" }
NotesWarning
list() assigns the values starting with the right-most parameter. If you are using plain variables, you don't have to worry about this. But if you are using arrays with indices you usually expect the order of the indices in the array the same you wrote in the list() from left to right; which it isn't. It's assigned in the reverse order.
|
more
Recently updated
more
Most requested
more
Last requests
|