Commonly used practical functions in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:49:20
Original
786 people have browsed it

1. For foreach, using $key=>$value and $i => $g have the same effect, and are not limited to key value

echo "
n";

$arr = array("one", "two", "three");

foreach ($arr as $i => $g) {

echo "Key: $i; Value: $g
n";

}

?>

2. array_fill () function

Fill the array with the given values

$a = array_fill(5, 6, 'banana');

print_r($a);

?>

Output:

Array ( [5] => banana [6] => banana [7] => banana [8] => banana [9] => banana [10] => banana )

3. Print array (PHP mode and JSON mode)

$data = array(

'firstname'=>'Tom',

'lastname'=>'Smith',

'age'=>40

);

print_r($data);

echo json_encode($data);

Output:

Array ( [firstname] => Tom [lastname] => Smith [age] => 40 )

{"firstname":"Tom","lastname":"Smith","age":40}

4. print_r($data,true); Only in this way can the array be written to the log www.2cto.com

$a = array ("a", "b", "c");
$c= print_r($a,true);

$logfile = "t.txt";

error_log("{$c}rnrn",3,$logfile);

Excerpted from Heda Li Xin’s Crazy Coding Life

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478354.htmlTechArticle1. For foreach, $key=$value and $i = $g have the same effect, not only Limited to key value ?php echo br /n; $arr = array(one, two, three); foreach ($arr as $i = $g) { echo Key:...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template