PHP Study : Syntax
Categories: PHP
PHP syntax and method study ✏️
1. list()
🔔 Like array(), this is not really a function, but a language construct.
list() is used to assign a list of variables in one operation. Strings can not be unpacked and list() expressions can not be completely empty.
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>
2. explode()
🔔 Split a string by a string
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator.
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
Parameters
separator
The boundary string.
string
The input string.
limit
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. If the limit parameter is negative, all components except the last -limit are returned. If the limit parameter is zero, then this is treated as 1.
3.
References
PHP Official Document
Penggu’s blog
Celestia’s blog
🌜 This is my personal study blog. If you find any errors or mistakes,
please feel free to point them out in the comments or via email. 💌
I would greatly appreciate it! 😄✨💛
Leave a comment