PHP에서 "=>"는 무엇을 의미합니까?
=>
다음 코드에서 연산자는 무엇을 의미합니까?
foreach ($user_list as $user => $pass)
코드는 PHP.net의 주석입니다. 사용자가 $user_list
, $user
또는 $ pass 의 값을 지정하지 않았습니다 . 나는 일반적으로 =>
같거나 더 크다는 것을 의미합니다.
그러나 나는 그것이 할당되지 않았기 때문에 여기에서 그 목적에 대해 확신하지 못합니다. 나는 코드를 다음과 같이 읽었다.
- 사용자 목록을 정수로 처리
- 각 사용자의 값이 암호보다 크거나 같도록
위의 내용은 이해가되지 않습니다.
=>
연관 배열의 구분 기호입니다. 해당 foreach 루프의 컨텍스트에서 배열의 키를에 할당 $user
하고 값을에 할당합니다 $pass
.
예:
$user_list = array(
'dave' => 'apassword',
'steve' => 'secr3t'
);
foreach ($user_list as $user => $pass) {
echo "{$user}'s pass is: {$pass}\n";
}
// Prints:
// "dave's pass is: apassword"
// "steve's pass is: secr3t"
숫자 인덱스 배열에도 사용할 수 있습니다.
예:
$foo = array('car', 'truck', 'van', 'bike', 'rickshaw');
foreach ($foo as $i => $type) {
echo "{$i}: {$type}\n";
}
// prints:
// 0: car
// 1: truck
// 2: van
// 3: bike
// 4: rickshaw
이는 $ user에게 키를 할당하고 $ pass에 변수를 할당하는 것을 의미합니다.
배열을 할당 할 때 다음과 같이합니다.
$array = array("key" => "value");
foreach 문에서 배열을 처리하는 데 동일한 기호를 사용합니다. '=>'는 키와 값을 연결합니다.
PHP 매뉴얼 에 따르면 '=>'는 키 / 값 쌍을 생성했습니다.
또한 같거나 큼은 '> ='와 반대입니다. PHP에서는보다 크거나 작은 기호가 항상 먼저 표시됩니다 : '> =', '<='.
그리고 부수적으로 두 번째 값을 제외하면 생각대로 작동하지 않습니다. 키만 제공하는 대신 실제로 값만 제공합니다.
$array = array("test" => "foo");
foreach($array as $key => $value)
{
echo $key . " : " . $value; // Echoes "test : foo"
}
foreach($array as $value)
{
echo $value; // Echoes "foo"
}
"a => b"와 같은 코드는 연관 배열 ( Perl 과 같은 일부 언어, 내가 올바르게 기억한다면 "hash"라고 부름)의 경우 'a'는 키이고 'b'는 값임을 의미합니다.
최소한 다음 문서를 살펴볼 수 있습니다.
Here, you are having an array, called $user_list
, and you will iterate over it, getting, for each line, the key of the line in $user
, and the corresponding value in $pass
.
For instance, this code:
$user_list = array(
'user1' => 'password1',
'user2' => 'password2',
);
foreach ($user_list as $user => $pass)
{
var_dump("user = $user and password = $pass");
}
Will get you this output:
string 'user = user1 and password = password1' (length=37)
string 'user = user2 and password = password2' (length=37)
(I'm using var_dump
to generate a nice output, that facilitates debuging; to get a normal output, you'd use echo
)
"Equal or greater" is the other way arround: "greater or equals", which is written, in PHP, like this; ">="
The Same thing for most languages derived from C: C++, JAVA, PHP, ...
As a piece of advice: If you are just starting with PHP, you should definitely spend some time (maybe a couple of hours, maybe even half a day or even a whole day) going through some parts of the manual :-)
It'd help you much!
An array in PHP is a map of keys to values:
$array = array();
$array["yellow"] = 3;
$array["green"] = 4;
If you want to do something with each key-value-pair in your array, you can use the foreach
control structure:
foreach ($array as $key => $value)
The $array variable is the array you will be using. The $key and $value variables will contain a key-value-pair in every iteration of the foreach
loop. In this example, they will first contain "yellow" and 3, then "green" and 4.
You can use an alternative notation if you don't care about the keys:
foreach ($array as $value)
Arrays in PHP are associative arrays (otherwise known as dictionaries or hashes) by default. If you don't explicitly assign a key to a value, the interpreter will silently do that for you. So, the expression you've got up there iterates through $user_list
, making the key available as $user
and the value available as $pass
as local variables in the body of the foreach
.
$user_list
is an array of data which when looped through can be split into it's name and value.
In this case it's name is $user
and it's value is $pass
.
참고URL : https://stackoverflow.com/questions/1241819/what-does-mean-in-php
'code' 카테고리의 다른 글
파이썬에서 한 줄 ftp 서버 (0) | 2020.09.02 |
---|---|
유형을 이동 만 가능하고 복사 불가능하게 만들 수 있습니까? (0) | 2020.09.01 |
HttpClient를 통해 REST API에 빈 본문 게시 (0) | 2020.09.01 |
BCL (기본 클래스 라이브러리) 대 FCL (프레임 워크 클래스 라이브러리) (0) | 2020.09.01 |
Java에서 고유 목록을 유지하는 방법은 무엇입니까? (0) | 2020.09.01 |